猜數字游戲。
我還是一個初學者,我只需要使用代碼中的那些運算子,我被禁止使用其他東西。程式給用戶 3 次嘗試猜測數字,但如果用戶超出范圍并寫入例如 -1 或 11,則程式應給出“錯誤”一詞,不應將其視為嘗試。
我了解您需要使用“for”回圈,
int a;
int b = 0;
for (a = 1; a < 11; a )
{
b = b a;
}
Console.WriteLine(b);
但我無法實作我的想法。如果您能提供幫助,我將不勝感激!
int a;
int aa;
a = 5;
Console.WriteLine("Guess my number from 1 to 10");
aa = Convert.ToInt32(Console.ReadLine());
int b;
for (b = 0; b < 2; b )
{
if (aa == a)
{
Console.WriteLine("Guessed");
}
else if (aa < a)
{
Console.WriteLine("more");
aa = Convert.ToInt32(Console.ReadLine());
}
else
{
if (aa > 10)
{
for ()
{
Console.WriteLine("error");
aa = Convert.ToInt32(Console.ReadLine());
}
}
else if (aa < 1)
{
Console.WriteLine("error");
aa = Convert.ToInt32(Console.ReadLine());
}
else
{
Console.WriteLine("less");
aa = Convert.ToInt32(Console.ReadLine());
}
}
}
uj5u.com熱心網友回復:
寫在手機上,但這應該可以。
int a;
int aa;
a = 5;
for (int b = 0; b < 2; b )
{
Console.WriteLine("Guess my number from 1 to 10");
aa = Convert.ToInt32(Console.ReadLine());
if(aa>10 ||aa<=0){
Console.WriteLine("error");
b--;
continue;
}
if (aa == a)
{
Console.WriteLine("Guessed");
break;
}
else if (aa < a)
{
Console.WriteLine("more");
}
else {
Console.WriteLine("less");
}
}
}
uj5u.com熱心網友回復:
我為你做了一個這樣的游戲。
const int number = 5;
const int attempts = 3;
for (var i = 1; i <= attempts; i )
{
Console.WriteLine($"Attempt number: {i}/{attempts}");
Console.WriteLine("Guess my number from 1 to 10");
if (!int.TryParse(Console.ReadLine(), out var guessedNumber))
{
Console.WriteLine("Wrong Format!");
i--;
continue;
}
if (number == guessedNumber)
{
Console.WriteLine("Guessed");
break;
}
if (guessedNumber > 10 || guessedNumber < 1)
{
Console.WriteLine("Error!");
i--;
}
if (i != attempts)
{
Console.WriteLine("Try again");
Console.WriteLine("*****************************");
}
else
Console.WriteLine("Failed");
}
Console.ReadLine(); //use for program wait there.
uj5u.com熱心網友回復:
我認為您的主要問題是獲取用戶輸入。你應該把它帶到它自己的方法:
private static int GetUserInput()
{
while(true) // could also be `for(;;)` if you are not allowed to use `while`
{
Console.WriteLine("Enter a number from 0 to 10:");
if(int.TryParse(Console.ReadLine(), out int userInput))
{
if( userInput >= 0 && userInput <= 10 )
{
return userInput;
}
}
Console.WriteLine("error.");
}
}
現在,您可以確保獲得有效猜測并專注于主要游戲流程。
uj5u.com熱心網友回復:
不需要多個for
回圈,您只需要一個,即主要的“嘗試回圈”。讓我們將其分解為程式的基本步驟:
- 獲取用戶輸入
- “邊界檢查”輸入
- 檢查用戶是否猜對
- 如果猜測不正確,請重復
我們希望最多重復 3 次。
使用它我們可以開始編程:
第 1 步:接受用戶輸入
Console.WriteLine("Guess the number (0-10):")
string guessInput = Console.ReadLine();
int guess = Convert.ToInt(guessInput);
第 2 步:邊界檢查:
// Read: "If guess is smaller than 0, or guess is greater than 10
if (guess < 0 || guess > 10)
{
Console.WriteLine("error");
guesses--;
}
第 3 步:檢查猜測是否正確:
if (guess == target)
{
Console.WriteLine("success");
}
第 4 步:重復
int target = 5;
for (int guesses = 0; guesses < 3; i )
{
// Code from steps 1 to 3 goes here
}
把它們放在一起:
(劇透警告!如果您想使用上述知識自行解決,請停止閱讀
int target = 5;
for (int guesses = 0; guesses < 3; i )
{
Console.WriteLine("Guess the number (0-10):");
string guessInput = Console.ReadLine();
int guess = Convert.ToInt(guessInput);
if (guess < 0 || guess > 10)
{
Console.WriteLine("error");
// Invalid input, so we're subtracting 1 from the total guesses
guesses--;
}
if (guess == target)
{
Console.WriteLine("Guessed");
// Congrats, now we want to stop the loop.
// We do this by invalidating the condition, i.e making 'guesses < 3' false
guesses = 10; // Any number larger than 3
}
if (guess > target)
{
Console.WriteLine("more");
}
else if (guess < target)
{
Console.WriteLine("less");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424742.html