我如何比较数组以获得我的宾果游戏中的结果? [英] How do I compare arrays to get a result in my bingo game?

查看:73
本文介绍了我如何比较数组以获得我的宾果游戏中的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我一直在努力寻找一个比较两个阵列但没有成功的解决方案。我创建了这个简单的宾果游戏,需要将宾果号码与用户输入数字进行比较。我正在寻找的是一个结果,说你得到了3个宾果游戏中的2个。你是宾果游戏中的一个号码。



我禁止使用linq并由我的老师包含()所以我必须用循环来解决它。



我真的很感激一个简单的答案,解释了教学方面做了什么,因为我非常想学习编程:)先谢谢你们。

Hi i'm new to programming and i have been trying so hard to find a solution to compare two arrays with no success. I've created this simple bingo game and need to compare the bingo numbers with the user input numbers. What I'm looking for is a result that says you got 2 out of 3 bingo numbers. you're one number from bingo.

Im forbidden to use linq and contain() by my teacher so i have to solve it with loops.

I would really appreciate a simple answer that explains pedagogically whats been done as i actaully want to learn programming :) Thanks in advance guys.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BINGO4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("welcome to the big-win-bingo");
            Console.WriteLine("enter three numbers: ");
            int i;


            Random rand = new Random();
            int ran = rand.Next(1, 3);

            int[] GenerateBingo = new int[3];
            for (i = 0; i < GenerateBingo.Length; i++)
            {
                GenerateBingo[i] = rand.Next(1, 3);
            }
            foreach (int utskrift in GenerateBingo)
            {

            }


            int[] UserInput = new int[3];
            for (int j = 0; j < UserInput.Length; j++)
            {
                UserInput[j] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("\nYou got the following numbers: ");
            foreach (int SaveArrayInput in UserInput)
            {
                Console.Write(" " + SaveArrayInput);
            }
            Console.WriteLine("\n\nPreass any button to see results... ");
            Console.ReadKey();

            foreach (int a in GenerateBingo)//here's the trouble.
                foreach (int b in UserInput)
                {
                    if (a == b)
                        Console.WriteLine("Matching numbers is: {0} ", i);
            Console.ReadLine();
        }
    }
}





我尝试了什么:





What I have tried:

foreach (int a in GenerateBingo)
                foreach (int b in UserInput)
                {
                    if (a == b)
                        Console.WriteLine("Matching numbers is: {0} ", i);
//and

for(int i = 0;i < array1.Length;i++)
          {
              string comp1 = array1[i];
              string comp2 = array2[i];
              if (comp1 == comp2)
              {
                 //Do something
              }
          }

推荐答案

我在代码中添加了注释。防止开发人员使用现成的工具是愚蠢的,但这是另一天的咆哮。我在这段代码中使用了一个你的老师可能不赞同的东西,那就是string.Join所以你可能需要删除它。



I've added comments inline with code. It is asinine to prevent a developer from using readily available tools but that is a rant for another day. I used one thing in this code that your teacher may not approve of and that would be string.Join so you probably need to remove that.

static void Main(string[] args)
        {
             Console.WriteLine("welcome to the big-win-bingo");
            Console.WriteLine("enter three numbers: ");
            int i;
 

            // Here you are getting a random number between 1 and 3...not very random but anyways
            Random rand = new Random();
            int ran = rand.Next(1, 3);
 
            int[] GenerateBingo = new int[3];
            // You've created an array of size 3 to store your generated bingo numbers.
            // For syntax reasons I strongly suggest you follow a standard code style convention and camel case your variable names
            // ex: generateBingo, althought I would say this is better names something like bingoNumbers.
            for (i = 0; i < GenerateBingo.Length; i++)
            {
                // Here you are getting the next random number between 1 and 3. Given this logic you are going to create a lot of bingo winners
                GenerateBingo[i] = rand.Next(1, 3);
            }
         

            // Same issue as before, camel case this variable
            int[] UserInput = new int[3];
            // Here you created an array of size 3 to capture a users input and convert it to int.
            for (int j = 0; j < UserInput.Length; j++)
            {
                // One thing to watch out here is if I type in A instead of an integer, this will explode.
                // If you don't know about try/catch blocks, now is a great time to learn. 
                // Also, look into Int32.TryParse 
                UserInput[j] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("\nYou got the following numbers: ");

            //Here you are just looping over UserInput and displaying what the person typed.
            foreach (int SaveArrayInput in UserInput)
            {
                Console.Write(" " + SaveArrayInput);
            }
            Console.WriteLine("\n\nPreass any button to see results... ");
            Console.ReadKey();


            // I think this is a no no for your teacher, I did this only so i can see what the generated bingo numbers were. Remove the string.join line.
            var bingonums = string.Join(",", GenerateBingo);
            Console.WriteLine("Bingo numbers are: " + bingonums);


            //So what i've done here is declare 2 variables, one tracks the number of matches found, the other tracks the 
            //  number of bingo numbers generated.
            var bingoCount = GenerateBingo.Length;
            var matchCount = 0;

            // Since you are matching bingo numbers to user input, I chose to loop over bingo first, I'm sure it could work by looping over
            //      the user input values first.
            foreach (var num in GenerateBingo)
            {
                // So the thing to note here is you want to loop over all user inputs within the bingo numbers in order to verify any numbers matched.
                //      This may create a false positive where if you have bingo numbers of 1, 2, 2...a user inputs 2. You could then have 2 matches out of 3 
                //      but I may not understand bingo correctly as it has been a long time.
                foreach (var input in UserInput)
                {
                    if (input == num)
                    {
                        Console.WriteLine("You got one of the bingo numbers " + input);
                        matchCount += 1;
                    }
                }
            }

            // Total bingo options - matches count shows number of options missed to acheieve bingo
            var missingMatchCount = bingoCount - matchCount;
            Console.WriteLine("You got {0} out of {1} matches, you are {2} number(s) away from bingo", matchCount, bingoCount, missingMatchCount);

            Console.ReadLine();
        }


尝试更换

try to replace
for(int i = 0;i < array1.Length;i++)
    {
        string comp1 = array1[i];
        string comp2 = array2[i];
        if (comp1 == comp2)
        {
           //Do something
        }
    }



by


by

for(int i = 0;i < array1.Length;i++)
    for(int j = 0;j < array2.Length;j++)
          {
              string comp1 = array1[i];
              string comp2 = array2[j];
              if (comp1 == comp2)
              {
                 //Do something
              }
          }


这篇关于我如何比较数组以获得我的宾果游戏中的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆