我是C#的新手,我们有一个活动来创建彩票游戏。

1个匹配的号码赢得了$ 10
2个匹配的号码赢得$ 100
3个匹配的号码不可用$ 1,000
3个匹配的号码依次赢得$ 10,000

我的代码出现问题,即使有2个匹配项或3个匹配数始终显示$ 10。任何帮助,将不胜感激。

以下是源代码。

using System;
using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace LotteryGame

{
class Program
{
    static void Main(string[] args)
    {
        // Matching numbers awards
        int rNumMatchOne = 10;
        int rNumMatchTwo = 100;
        int rNumMatchThree = 1000;
        int rNumMatchFour = 10000;

        // Generate random numbers
        Random randomNum = new Random();

        // Integers Declaration
        int rNum1;
        int rNum2;
        int rNum3;
        int rNumIput;
        int guessNum;

        // Arrays Declartion
        int[] guessNumMatch = new int[3];
        int[] guessNumSort = new int[3];
        int[] guessInput = new int[3];

        // Restrict inputs between 1 and 4 only
        rNum1 = randomNum.Next(1, 5);
        rNum2 = randomNum.Next(1, 5);
        rNum3 = randomNum.Next(1, 5);

        Console.Write("C# Lottery Game\n\n");

        Array.Sort(guessNumSort); // sort random numbers
        // Guess number input loop
        for (rNumIput = 0; rNumIput < 3; rNumIput++)
        {
            Console.Write("Guess Number " + (rNumIput + 1) + ": ");
            guessNum = Convert.ToInt32(Console.ReadLine());

            // Invalid input between 1 and 4 program will loop back and enter correct number
            while (guessNum < 1 || guessNum > 4)
            {
                Console.WriteLine("\n");
                Console.WriteLine("Invalid Number. Please enter number between 1 and 4. \n");
                Console.Write("Guess Number " + (rNumIput + 1) + ": ");
                guessNum = Convert.ToInt32(Console.ReadLine());
            }
            guessNumMatch[rNumIput] = guessNum;
            guessNumSort[rNumIput] = guessNum;
        }
        Array.Sort(guessNumSort);
        // Display random numbers and entered numbers
        Console.WriteLine();
        Console.WriteLine("Generated random numbers are   : " + rNum1 + " | " + rNum2 + " | " + rNum3);
        Console.WriteLine("Numbers you entered are        : " + guessNumMatch[0] + " | " + guessNumMatch[1] + " | " + guessNumMatch[2]);


        // Matching 1 number
        if (guessNumMatch[0] == rNum1 || guessNumMatch[1] == rNum2 || guessNumMatch[2] == rNum3)
        {
            Console.WriteLine("\n");
            Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchOne);
        }
        // Matching 2 numbers
        else if ((guessNumMatch[0] == rNum1 && guessNumMatch[1] == rNum2) || (guessNumMatch[1] == rNum2 && guessNumMatch[2] == rNum3))
        {
            Console.WriteLine("\n");
            Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchTwo);
        }
        // Matching 3 numbers not in order
        else if (guessNumSort[0] == guessInput[0] && guessNumSort[1] == guessInput[1] && guessNumSort[2] == guessInput[2])
        {
            Console.WriteLine("\n");
            Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchThree);
        }
        // Matching 3 numbers exact order
        else if (guessNumMatch[0] == rNum1 && guessNumMatch[1] == rNum2 && guessNumMatch[2] == rNum3)
        {
            Console.WriteLine("\n");
            Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchFour);
        }
        else // No matching numbers
        {
            Console.WriteLine("\n");
            Console.WriteLine("SORRY, NO MATCHING NUMBERS FOUND! ");
        }
        Console.WriteLine("\n");
        Console.WriteLine("PRESS ANY KEY TO EXIT PROGRAM ");
        Console.ReadKey();
    }
}
}

最佳答案

反转if语句的顺序。首先检查3个数字是否按顺序匹配,然后检查3个,2个,1个和最后一个不匹配。

否则,即使有多个匹配项,第一个if语句也将为true。

关于c# - C#彩票比较结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52100222/

10-08 22:15