C#彩票比较结果

时间:2018-08-30 15:05:07

标签: c#

我是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();
    }
}
}

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

如果至少其中一个数字正确,则您的第一个if语句将评估为true。例如,如果用户正确猜出第二个和第三个数字,则guessNumMatch[1] == rNum2将得出trueif(false || true || true)的计算结果为true,因此该语句得以执行。其他if条语句将被跳过。

一种解决方案(就像Attersson击败我的那样)是将if语句反转-检查所有3个是否为真,然后检查2个是否为真,等等。