C# 彩票比较结果

标签 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();
    }
}
}

最佳答案

颠倒 if 语句的顺序。首先检查 3 个数字是否按顺序匹配,然后检查 3、2、1,最后不匹配。

否则,即使有超过 1 个匹配项,第一个 if 语句也会命中 true。

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

相关文章:

c# - 如何压缩一个目录的内容,除了一个子目录?

c# - Visual Studio 项目系统遇到错误

c# - 如何从 ConcurrentBag<> 中删除单个特定对象?

c# - 将信息记录到 azure webjobs 日志文件中?

C# 调用 Jupyter Notebook

c# - 使用cancelingtoken为用户定义的功能设置超时

c# - 如何下载并运行.exe文件c#

c# - 需要在 c# 中制作 .lib、.h 和 .dll 的包装器

c# - 从 ASP.NET 5 Controller VS 2015 获取 wwwroot 文件夹路径

c# - XML 中的非法字符