![](/img/trans.png)
[英]Trying to make my 2D character double jump - it's not working and I don't know why?
[英]my guessing game is not working and i don´t know why
我正在做一個猜謎游戲,我需要在學校進行改進,我需要制作它,以便我的游戲可以進行更多輪次。 但是我有一個問題,程序不想退出,即使我“發出退出命令”它也只會開始新一輪。 現在的問題是我的游戲甚至不想開始,我不知道為什么。 我已經嘗試了很多方法來嘗試修復它,但沒有任何效果,我需要改為使用 do while 循環。 而且我想我知道如何在沒有大問題的情況下做到這一點。 這是我的代碼
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();
}
}
}
它不起作用的原因是您將響應轉換為大寫。 但是,您不會檢查單詞的大寫版本。
所以if (response == "Ja")
應該是 if (response == "JA")
僅作記錄,通常當您在 C# 中比較string
時,尋找讓您也提供StringComparison
的方法。
例如,如果我想檢查一個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!
順便說一句,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
您的字符串中有換行符,這使您的代碼無法編譯。 在 C# 中,您不能像這樣中斷字符串:
// This does not compile
string str = "my string on
several lines";
我/* 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();
}
}
}
但是,您可以使用逐字字符串文字( @"myText"
)來編寫它:
// This compiles
Console.WriteLine(@"gissa talet
Du ska nu gissa ett tal mellan 1 ocn 100, så varsågod..
skriv in ett tal");
旁注:如果您猜對了數字,您仍然會輸入“ guess is close ”部分(如guess-number == 0
,即<= 3
)。
請參閱下面屏幕截圖中的紅線:
除了您的其他問題之外,您還有一個隱藏在此處某處的無限循環...
//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");
}
我相信你可以從這里自己弄清楚!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.