为什么彩票代码的输出不起作用?

时间:2018-06-29 15:15:45

标签: c# random input int compare

我应该在c#(Microsoft Visual Studio 2017)中编写一个代码,让用户输入六个数字,然后将它们与六个随机生成的数字(无重复)进行比较。

如果用户有一个匹配项,那么他应该会收到一条消息,说他有一个匹配项,而对于两三个匹配项则显示不同的消息,依此类推。

这是我到目前为止得到的:

static bool isValueInArray(int value, int[] a)
    {
        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] == value)
            {
                return true;
            }
        }

        return false;
    }

  static void Main(string[] args)
    {
        int min = 0;
        int max = 6;
        Random randnum = new Random();//random number generator
        int[] numbers = new int[6];// generates six numbers
        for (int i = 0; i < numbers.Length; i++)
        {
            numbers[i] = randnum.Next(min, max);//sets the numbers between 0 to 6
            if(isValueInArray( i, numbers))
            {
                numbers[i] = randnum.Next(min, max);
            }
        }

        try
        {
            Console.WriteLine("Hello and Welcome to our little game of lottery! lets see just how much luck you got!"); // greetings and instructions
            Console.WriteLine("You'll now get to choose 6 different numbers between 0 to 6 to play with.");
            Console.WriteLine("Go ahead and type them in.");

            int[] lottery = new int[6];

            for (int i = 0; i < lottery.Length; i++)
            {
                lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
                if (lottery[i] > 6)//checking if the numbers fit between 0 and 6
                {
                    Console.WriteLine("whoops! the number you enetered isn't in range! please try again ^^");
                    break;
                }

                int x = 6;
                for (int a = 0; a < lottery.Length; a++)
                {
                    for (int b = 0; b < numbers.Length; b++)
                    {
                        if (lottery[a] == numbers[b])
                        {
                            a++;
                            x--;
                            if (x == 6)
                            {
                                Console.WriteLine("six match");
                                break;
                            }
                            else if (x == 5)
                            {
                                Console.WriteLine("five match");
                                break;
                            }
                            else if (x == 4)
                            {
                                Console.WriteLine("four match");
                                break;
                            }
                            else if (x == 3)
                            {
                                Console.WriteLine("three match");
                                break;
                            }
                            else if (x == 2)
                            {
                                Console.WriteLine("two match");
                                break;
                            }
                            else if (x == 1)
                            {
                                Console.WriteLine("one match");
                                break;
                            }

                        }
                    }
                }
            }
        }

        catch (FormatException)// checking if the input is in char format
        {
            Console.WriteLine("only numbers please!");
        }
    }

我的问题是输出。该程序似乎遍历了所有“ else if”选项并打印所有选项,而不是仅选择和打印其中一个选项。

3 个答案:

答案 0 :(得分:4)

您将所有内容混合在一起。写下步骤:

  1. 生成数字
  2. 从用户那里获取输入
  3. 计算比赛次数
  4. 打印结果

每个步骤都应单独执行。

// These should be the min/max lottery numbers
int min = 1;
int max = 100;

int numberOfLotteryNumbers = 6;
// Renamed for clarity
int[] lotteryNumbers = new int[numberOfLotteryNumbers];
int[] userNumbers = new int[numberOfLotteryNumbers];

// Step 1 - generate numbers
for (int i = 0; i < lotteryNumbers.Length; i++) {
    int randomNumber;
    do {
       randomNumber = randnum.Next(min, max);
    } while (isValueInArray(randomNumber, lotteryNumbers));
    lotteryNumbers[i] = randomNumber;
}

// Step 2 - get numbers from user
for (int i = 0; i < lottery.Length; i++) {
    int userInput;
    do {
        userInput = int.Parse(Console.ReadLine());
    } while (userInput < min || userInput > max || isValueInArray(userInput, userNumbers));
    userNumbers[i] = userInput;
}

// Step 3 - calc matches
int matches = 0;
for (int i = 0; i < userNumbers.Length; i++) {
    if (isValueInArray(userNumbers[i], lotteryNumbers) {
        matches += 1;
    }
}

// Step 4 - print results
Console.WriteLine("There are {0} matches.", matches);

答案 1 :(得分:1)

您可以使用计数器来实现您的目标。只需在匹配时增加计数器值即可:

int counter = 0;
for (int i = 0; i < lottery.Length; i++)
{ 
  // .. 
  if (x == number)
  {
      counter++; 
      break;
  } 
  // ..
} 
Console.WriteLine("You have " + counter + " matches");

答案 2 :(得分:1)

您可能需要重新排列代码块,以便首先获取用户输入,首先计算匹配数,然后将结果显示为以下代码:

注意:您要确保随机生成的数字中的唯一数字无法按预期工作,您正在传递“ i”,而您可能希望将“ numbers [i]”传递给isValueInArray”抛开这个想法,因为您需要6个数字,所以总是在数组中以0-5结束。

            int min = 0;
            int max = 6;
            Random randnum = new Random();//random number generator
            int[] numbers = new int[6];// generates six numbers
            for (int i = 0; i < numbers.Length; i++)
            {
                numbers[i] = randnum.Next(min, max);//sets the numbers between 0 to 6
                if (isValueInArray(i, numbers))
                {
                    numbers[i] = randnum.Next(min, max);
                }
            }

            try
            {
                Console.WriteLine("Hello and Welcome to our little game of lottery! lets see just how much luck you got!"); // greetings and instructions
                Console.WriteLine("You'll now get to choose 6 different numbers between 0 to 6 to play with.");
                Console.WriteLine("Go ahead and type them in.");

                int[] lottery = new int[6];

                int x = 0;
                //read user numbers
                for (int i = 0; i < lottery.Length; i++)
                {
                    lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
                    while (lottery[i] > 6)//checking if the numbers fit between 0 and 6
                    {
                        Console.WriteLine("whoops! the number you enetered isn't in range! please try again ^^");
                        lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
                    }
                }

                //count number of matches
                for (int a = 0; a < lottery.Length; a++)
                {
                    for (int b = 0; b < numbers.Length; b++)
                    {
                        if (lottery[a] == numbers[b])
                        {
                            //a++;
                            x++;
                            break;
                        }
                    }
                }

                //display results
                if (x == 6)
                {
                    Console.WriteLine("six matches");
                }
                else if (x == 5)
                {
                    Console.WriteLine("five matches");
                }
                else if (x == 4)
                {
                    Console.WriteLine("four matches");
                }
                else if (x == 3)
                {
                    Console.WriteLine("three matches");
                }
                else if (x == 2)
                {
                    Console.WriteLine("two matches");
                }
                else if (x == 1)
                {
                    Console.WriteLine("one match");
                }

            }

            catch (FormatException)// checking if the input is in char format
            {
                Console.WriteLine("only numbers please!");
            }
            Console.Read();
        }