C#if语句在控制台游戏中不起作用(我是初学者,所以可能完全错了)
C#if语句在控制台游戏中不起作用(我是初学者,所以可能完全错了),c#,C#,if语句应该打印我是否在玩石头、布、剪刀的游戏中赢了、输了或画了。但这并没有发生 预期结果: 请输入石头、布或剪刀 计算机已选择: 纸 你赢了 实际结果: 请输入石头、布或剪刀 计算机已选择: 纸 一般来说,我对编程没有最好的理解,否则我可能会自己解决这个问题 我开始学习C#的原因是因为我现在在学校学习C#,所以我认为在家学习也会让我有更好的机会快速理解C# 这是我的密码,有什么问题吗?(请尽量简单,因为我几天前才真正开始学习C#) 问题就在这条线上: Console.WriteLine(rps
if
语句应该打印我是否在玩石头、布、剪刀的游戏中赢了、输了或画了。但这并没有发生
预期结果:
请输入石头、布或剪刀
计算机已选择:
纸
你赢了
实际结果:
请输入石头、布或剪刀
计算机已选择:
纸
一般来说,我对编程没有最好的理解,否则我可能会自己解决这个问题
我开始学习C#的原因是因为我现在在学校学习C#,所以我认为在家学习也会让我有更好的机会快速理解C#
这是我的密码,有什么问题吗?(请尽量简单,因为我几天前才真正开始学习C#)
问题就在这条线上:
Console.WriteLine(rpsOpt[new Random().Next(0, rpsOpt.Length)]);
您正在控制台输出上编写计算机选项,但实际上并没有将该选项存储在任何位置
string finalOpt = Console.ReadLine();
然后,上面的行等待另一个用户输入,您可能只是再次按下Enter
键,这将导致一个空字符串存储在finalOpt
变量中
之后,您将用户的第一选择与finalOpt
变量中的空字符串进行比较,并且您的if
条件都不能处理这种情况。这就是为什么你看不到任何输出
为了解决这个问题,您需要首先将随机选择存储到变量中,然后将其打印到控制台:
Console.WriteLine("The Computer has chosen:");
string finalOpt = rpsOpt[new Random().Next(0, rpsOpt.Length)];
Console.WriteLine(finalOpt);
问题就在这条线上:
Console.WriteLine(rpsOpt[new Random().Next(0, rpsOpt.Length)]);
您正在控制台输出上编写计算机选项,但实际上并没有将该选项存储在任何位置
string finalOpt = Console.ReadLine();
然后,上面的行等待另一个用户输入,您可能只是再次按下Enter
键,这将导致一个空字符串存储在finalOpt
变量中
之后,您将用户的第一选择与finalOpt
变量中的空字符串进行比较,并且您的if
条件都不能处理这种情况。这就是为什么你看不到任何输出
为了解决这个问题,您需要首先将随机选择存储到变量中,然后将其打印到控制台:
Console.WriteLine("The Computer has chosen:");
string finalOpt = rpsOpt[new Random().Next(0, rpsOpt.Length)];
Console.WriteLine(finalOpt);
以下代码行中存在错误
Console.WriteLine(rpsOpt[new Random().Next(0, rpsOpt.Length)]);
string finalOpt = Console.ReadLine();
您可以打印出随机的计算机选择。打印出所选内容后,您正在等待输入。因此字符串finalOpt
包含用户输入,而不是随机选择
您必须将随机选择放入finalOpt
字符串并打印出该字符串
string finalOpt = rpsOpt[new Random().Next(0, rpsOpt.Length)];
Console.WriteLine(finalOpt);
以下代码行中存在错误
Console.WriteLine(rpsOpt[new Random().Next(0, rpsOpt.Length)]);
string finalOpt = Console.ReadLine();
您可以打印出随机的计算机选择。打印出所选内容后,您正在等待输入。因此字符串finalOpt
包含用户输入,而不是随机选择
您必须将随机选择放入finalOpt
字符串并打印出该字符串
string finalOpt = rpsOpt[new Random().Next(0, rpsOpt.Length)];
Console.WriteLine(finalOpt);
您的代码在以下几行出现问题: 我更正了你的代码。 试试看:
using System;
namespace SimpleGame
{
class Program
{
public delegate int SquareDelegate(int number);
static void Main(string[] args)
{
string appName = "Rock, Paper, Scissors";
string appVersion = "1.0.0";
string author = "Rhys Keown";
Console.WriteLine("{0}, Version {1}, Made By {2}", appName, appVersion, author);
Console.WriteLine();
Console.WriteLine("Please press any key to begin the game!");
Console.ReadKey();
// Ask for the user to input either Rock, Paper, Scissors
Console.WriteLine("Please input either Rock, Paper or Scissors");
string userOpt = Console.ReadLine();
// Random option completed by the computer.
string[] rpsOpt = new string[] { "Rock", "Paper", "Scissors" };
Console.WriteLine("The computer has chosen:");
// Computer Chosen value - Stored to 'b'
string b = rpsOpt[new Random().Next(0, rpsOpt.Length)];
//string finalOpt = Console.ReadLine();
// If the selection of the user beats the computer then You Win!
// is printed and you are asked if you want to have another game.
string a = userOpt;
if (b == "Rock" && a == "Rock")
{
Console.WriteLine("You have drawn");
}
else if (b == "Paper" && a == "Paper")
{
Console.WriteLine("You have drawn");
}
else if (b == "Scissors" && a == "Scissors")
{
Console.WriteLine("You have drawn");
}
else if (b == "Rock" && a == "Scissors")
{
Console.WriteLine("You have lost");
}
else if (b == "Rock" && a == "Paper")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Rock")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Paper")
{
Console.WriteLine("You have lost");
}
else if (b == "Paper" && a == "Scissors")
{
Console.WriteLine("You have won!!");
}
else if (b == "Paper" && a == "Rock")
{
Console.WriteLine("You have lost");
}
else (b == "Paper" && a == "Rock")
{
Console.WriteLine("Something Wrong When Computing results..");
}
Console.ReadKey();
}
}
}
输出:
using System;
namespace SimpleGame
{
class Program
{
public delegate int SquareDelegate(int number);
static void Main(string[] args)
{
string appName = "Rock, Paper, Scissors";
string appVersion = "1.0.0";
string author = "Rhys Keown";
Console.WriteLine("{0}, Version {1}, Made By {2}", appName, appVersion, author);
Console.WriteLine();
Console.WriteLine("Please press any key to begin the game!");
Console.ReadKey();
// Ask for the user to input either Rock, Paper, Scissors
Console.WriteLine("Please input either Rock, Paper or Scissors");
string userOpt = Console.ReadLine();
// Random option completed by the computer.
string[] rpsOpt = new string[] { "Rock", "Paper", "Scissors" };
Console.WriteLine("The computer has chosen:");
// Computer Chosen value - Stored to 'b'
string b = rpsOpt[new Random().Next(0, rpsOpt.Length)];
//string finalOpt = Console.ReadLine();
// If the selection of the user beats the computer then You Win!
// is printed and you are asked if you want to have another game.
string a = userOpt;
if (b == "Rock" && a == "Rock")
{
Console.WriteLine("You have drawn");
}
else if (b == "Paper" && a == "Paper")
{
Console.WriteLine("You have drawn");
}
else if (b == "Scissors" && a == "Scissors")
{
Console.WriteLine("You have drawn");
}
else if (b == "Rock" && a == "Scissors")
{
Console.WriteLine("You have lost");
}
else if (b == "Rock" && a == "Paper")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Rock")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Paper")
{
Console.WriteLine("You have lost");
}
else if (b == "Paper" && a == "Scissors")
{
Console.WriteLine("You have won!!");
}
else if (b == "Paper" && a == "Rock")
{
Console.WriteLine("You have lost");
}
else (b == "Paper" && a == "Rock")
{
Console.WriteLine("Something Wrong When Computing results..");
}
Console.ReadKey();
}
}
}
您的代码在以下几行出现问题: 我更正了你的代码。 试试看:
using System;
namespace SimpleGame
{
class Program
{
public delegate int SquareDelegate(int number);
static void Main(string[] args)
{
string appName = "Rock, Paper, Scissors";
string appVersion = "1.0.0";
string author = "Rhys Keown";
Console.WriteLine("{0}, Version {1}, Made By {2}", appName, appVersion, author);
Console.WriteLine();
Console.WriteLine("Please press any key to begin the game!");
Console.ReadKey();
// Ask for the user to input either Rock, Paper, Scissors
Console.WriteLine("Please input either Rock, Paper or Scissors");
string userOpt = Console.ReadLine();
// Random option completed by the computer.
string[] rpsOpt = new string[] { "Rock", "Paper", "Scissors" };
Console.WriteLine("The computer has chosen:");
// Computer Chosen value - Stored to 'b'
string b = rpsOpt[new Random().Next(0, rpsOpt.Length)];
//string finalOpt = Console.ReadLine();
// If the selection of the user beats the computer then You Win!
// is printed and you are asked if you want to have another game.
string a = userOpt;
if (b == "Rock" && a == "Rock")
{
Console.WriteLine("You have drawn");
}
else if (b == "Paper" && a == "Paper")
{
Console.WriteLine("You have drawn");
}
else if (b == "Scissors" && a == "Scissors")
{
Console.WriteLine("You have drawn");
}
else if (b == "Rock" && a == "Scissors")
{
Console.WriteLine("You have lost");
}
else if (b == "Rock" && a == "Paper")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Rock")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Paper")
{
Console.WriteLine("You have lost");
}
else if (b == "Paper" && a == "Scissors")
{
Console.WriteLine("You have won!!");
}
else if (b == "Paper" && a == "Rock")
{
Console.WriteLine("You have lost");
}
else (b == "Paper" && a == "Rock")
{
Console.WriteLine("Something Wrong When Computing results..");
}
Console.ReadKey();
}
}
}
输出:
using System;
namespace SimpleGame
{
class Program
{
public delegate int SquareDelegate(int number);
static void Main(string[] args)
{
string appName = "Rock, Paper, Scissors";
string appVersion = "1.0.0";
string author = "Rhys Keown";
Console.WriteLine("{0}, Version {1}, Made By {2}", appName, appVersion, author);
Console.WriteLine();
Console.WriteLine("Please press any key to begin the game!");
Console.ReadKey();
// Ask for the user to input either Rock, Paper, Scissors
Console.WriteLine("Please input either Rock, Paper or Scissors");
string userOpt = Console.ReadLine();
// Random option completed by the computer.
string[] rpsOpt = new string[] { "Rock", "Paper", "Scissors" };
Console.WriteLine("The computer has chosen:");
// Computer Chosen value - Stored to 'b'
string b = rpsOpt[new Random().Next(0, rpsOpt.Length)];
//string finalOpt = Console.ReadLine();
// If the selection of the user beats the computer then You Win!
// is printed and you are asked if you want to have another game.
string a = userOpt;
if (b == "Rock" && a == "Rock")
{
Console.WriteLine("You have drawn");
}
else if (b == "Paper" && a == "Paper")
{
Console.WriteLine("You have drawn");
}
else if (b == "Scissors" && a == "Scissors")
{
Console.WriteLine("You have drawn");
}
else if (b == "Rock" && a == "Scissors")
{
Console.WriteLine("You have lost");
}
else if (b == "Rock" && a == "Paper")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Rock")
{
Console.WriteLine("You have won!!");
}
else if (b == "Scissors" && a == "Paper")
{
Console.WriteLine("You have lost");
}
else if (b == "Paper" && a == "Scissors")
{
Console.WriteLine("You have won!!");
}
else if (b == "Paper" && a == "Rock")
{
Console.WriteLine("You have lost");
}
else (b == "Paper" && a == "Rock")
{
Console.WriteLine("Something Wrong When Computing results..");
}
Console.ReadKey();
}
}
}
“但这并没有发生”并不是很具体。请告诉我们您输入了什么,预期结果是什么,以及您的代码是如何无法实现的?
b
如果用户在第二次提示下再次输入,则只能使用石头、布或剪刀。编程时首先需要学习两件事,它们是编程和调试。在if
语句之前的某个地方放置一个断点,然后逐步遍历代码。“这并没有发生”并不是很具体。请告诉我们您输入了什么,预期结果是什么,以及您的代码是如何无法实现的?b
如果用户在第二次提示下再次输入,则只能使用石头、布或剪刀。编程时首先需要学习两件事,它们是编程和调试。在if
语句之前的某个地方放置一个断点,然后逐步遍历代码。这个答案比你之前发布的答案好多少?这实际上解释了问题所在,而不仅仅是更正代码?@Crowcoder-我刚刚看到了问题,并将问题中的代码复制/粘贴到visual studio,找到了问题所在…然后解决问题并发布答案…这个答案比在你之前发布的那篇文章实际上解释了问题所在,而不仅仅是更正代码?@Crowcoder-我只是看到了问题,并将问题中的代码复制/粘贴到visual studio,找到了问题所在……然后解决问题并发布答案。。。。