简体   繁体   English

我的猜谜游戏不起作用,我不知道为什么

[英]my guessing game is not working and i don´t know why

I'm working on a guessing game that I need to improve for school, and I need to make it so that my game can make more rounds.我正在做一个猜谜游戏,我需要在学校进行改进,我需要制作它,以便我的游戏可以进行更多轮次。 But I hav a problem, the program doesn´t want to quit, it only starts a new round even if I "make the quit command".但是我有一个问题,程序不想退出,即使我“发出退出命令”它也只会开始新一轮。 The problem now is that my game doesn´t even want to start and I have no idea why.现在的问题是我的游戏甚至不想开始,我不知道为什么。 I have tried so many things to try to fix it and nothing works and I need to make it to a do while loop instead.我已经尝试了很多方法来尝试修复它,但没有任何效果,我需要改为使用 do while 循环。 And I think I know how to do that without big problems.而且我想我知道如何在没有大问题的情况下做到这一点。 Here is my code这是我的代码

using System;

 namespace MyFirstProgram
 {
     class Program
     {
         static void Main(string[] args)
         {
             Random random = new Random();
             bool playAgain = true;
             int min = 1;
             int max = 100;
             int guess;
             int number;
             int guesses;
             String response;

             while (playAgain)
             {
                 guess = 0;
                 guesses = 0;
                 response = "";
                 number = random.Next(min, max + 1);

                 while (guess != number)
                 {
                     //opening quote
                     Console.ForegroundColor = ConsoleColor.Green;
                     Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 
 ocn 100, så varsågod..\nskriv in ett tal");
                     guess = Convert.ToInt32(Console.ReadLine());
                     Console.WriteLine("Guess: " + guess);

                     //when guess is over 100
                     if (guess > 100)
                     {
                          Console.ForegroundColor = ConsoleColor.DarkRed;
                          Console.WriteLine("Du måste skriva in ett tal mellan 1 och 
 100!");
                     }
                     else
                     {

                         //if guess is to small
                         if (guess > number)
                         {
                             Console.ForegroundColor = ConsoleColor.DarkRed;
                             Console.WriteLine("Ditt tal är för litet. gissa på ett 
 större tal");
                         }

                         //if guess is to large
                         else if (guess < number)
                         {
                             Console.ForegroundColor = ConsoleColor.DarkRed;
                             Console.WriteLine("Ditt tal är för stort. gissa på ett 
 mindre tal.");
                         }

                         //when your guess is close but not right
                         if (Math.Abs(guess - number) <= 3)
                         {
                             Console.ForegroundColor = ConsoleColor.Red;
                             Console.WriteLine("Du är dock nära och det bränns");
                         }
                     }
                         guesses++;
                 }
                 //when you won
                 Console.ForegroundColor = ConsoleColor.Yellow;
                 Console.WriteLine("Grattis du gissa rätt,\n talet är" + " " + number);
                 Console.WriteLine("gissning" + " " + guesses);

                 //playagain command
                 Console.ForegroundColor = ConsoleColor.Cyan;
                 Console.WriteLine("vill du spela igen? (Ja/Nej): ");
                 response = Console.ReadLine();
                 response = response.ToUpper();

                 if (response == "Ja")
                 {
                     playAgain = true;
                 }
                 else
                 {
                     playAgain = false;
                 }
             }
             //end quote
             Console.WriteLine("tack för att du spela");

             Console.ReadKey();
         }
     }
 }    

The reason that it doesn't work is that you convert the response to upper case.它不起作用的原因是您将响应转换为大写。 However you don't check for the upper case version of the word.但是,您不会检查单词的大写版本。

So if (response == "Ja") should be if (response == "JA")所以if (response == "Ja")应该是 if (response == "JA")

Just for the record, usually when you compare string in C#, seek for ways that let you also provide StringComparison .仅作记录,通常当您在 C# 中比较string时,寻找让您也提供StringComparison的方法。

For example, if I want to check if a string is Ja , but without considering character case, I can write something like this:例如,如果我想检查一个string是否为Ja ,但不考虑字符大小写,我可以这样写:

    public static void Main()
    {
        string input = "JA";

        if (input.Equals("Ja", StringComparison.InvariantCultureIgnoreCase))
        {
            Console.WriteLine("They're the same!");
        }
    }

// result: They're the same!

By the way, C# Dictionary also supports this.顺便说一句,C# Dictionary也支持这一点。

    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
        
        dict.Add("Ja", "Peko");
        
        Console.WriteLine(dict["JA"]);
    }

// result: Peko

You have line breaks in your strings, which make your code unable to compile.您的字符串中有换行符,这使您的代码无法编译。 In C#, you can't break strings like this:在 C# 中,您不能像这样中断字符串:

// This does not compile
string str = "my string on
several lines";

I tagged involved lines with /* here ---> */ comments below:/* here ---> */评论如下:

using System;

namespace MyFirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            bool playAgain = true;
            int min = 1;
            int max = 100;
            int guess;
            int number;
            int guesses;
            String response;

            while (playAgain)
            {
                guess = 0;
                guesses = 0;
                response = "";
                number = random.Next(min, max + 1);

                while (guess != number)
                {
                    //opening quote
                    Console.ForegroundColor = ConsoleColor.Green;
/* here ---> */     Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så varsågod..\nskriv in ett tal");
                    guess = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Guess: " + guess);

                    //when guess is over 100
                    if (guess > 100)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkRed;
/* here ---> */         Console.WriteLine("Du måste skriva in ett tal mellan 1 och 100!");
                    }
                    else
                    {
                        //if guess is to small
                        if (guess > number)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkRed;
/* here ---> */             Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
                        }
                        //if guess is to large
                        else if (guess < number)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkRed;
/* here ---> */             Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal.");
                        }
                        //when your guess is close but not right
                        if (Math.Abs(guess - number) <= 3)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Du är dock nära och det bränns");
                        }
                    }
                    guesses++;
                }
                //when you won
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Grattis du gissa rätt,\n talet är" + " " + number);
                Console.WriteLine("gissning" + " " + guesses);

                //playagain command
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("vill du spela igen? (Ja/Nej): ");
                response = Console.ReadLine();
                response = response.ToUpper();

                if (response == "JA")
                {
                    playAgain = true;
                }
                else
                {
                    playAgain = false;
                }
            }
            //end quote
            Console.WriteLine("tack för att du spela");

            Console.ReadKey();
        }
    }
}

You could however write it with verbatim string literal ( @"myText" ):但是,您可以使用逐字字符串文字( @"myText" )来编写它:

// This compiles
Console.WriteLine(@"gissa talet
Du ska nu gissa ett tal mellan 1 ocn 100, så varsågod..
skriv in ett tal");

Side note: if you guess the right number, you still enter the " guess is close " part (as guess-number == 0 which is <= 3 ).旁注:如果您猜对了数字,您仍然会输入“ guess is close ”部分(如guess-number == 0 ,即<= 3 )。

See red line in below screenshot:请参阅下面屏幕截图中的红线:

不错的猜测

Besides your other problems you have a infinite loop hidden somewhere here...除了您的其他问题之外,您还有一个隐藏在此处某处的无限循环...

    //if guess is to small
if (guess > number)
{
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
}

//if guess is to large
else if (guess < number)
{
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal.");
}

//when your guess is close but not right
if (Math.Abs(guess - number) <= 3)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Du är dock nära och det bränns");
}

I am sure you can figure it out by yourself from here on!我相信你可以从这里自己弄清楚!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM