你好,我正在用C#编写一个程序,该程序需要一个数字并要求用户输入数字,一旦用户输入了数字,程序就会检查它是否与原始数字匹配,如果不匹配,则应该告诉用户是否答案小于或大于,但是我的if语句不起作用。
//Boolean if user passes
Boolean Userpassed;
// Declares Answer Variable
int answer = 6;
// Asks user for a number
Console.WriteLine("Enter a number between 1 and 10");
// Displays Message
Console.ReadLine();
// Takes User Input as String
string useranswer = Console.ReadLine();
// Declares integer for actual integer
int userinput;
// User string parsed to Int
userinput = Convert.ToInt32(Console.Read());
// Displays Message
Console.ReadLine();
// Checks if input is equal to answer
if (userinput == answer)
{
// User passes
Console.WriteLine("Congratulations, you have passed");
}
// Checks if input is greater than answer
else if (userinput > answer)
{
// User Fails
Console.WriteLine("The actual answer is less than what you entered");
}
// Checks if input is less than answer
else if (userinput < answer)
{
// User Fails
Console.WriteLine("The actual answer is greater than what you entered");
}
// Displays Message
Console.ReadLine();
第一个if语句不起作用,因为当我输入6时,它只是传递给else循环。然后,如果输入其他数字,则不会超过第二个else循环。我该如何解决这个问题?
答案 0 :(得分:1)
您必须Parse
输入行而不是Convert
输入单个字符,即代替
Console.WriteLine("Enter a number between 1 and 10");
// Displays Message
// Console.ReadLine(); <== Remove this line
// Takes User Input as String
string useranswer = Console.ReadLine();
// Declares integer for actual integer
int userinput;
// User string parsed to Int
userinput = Convert.ToInt32(useranswer);
您应该输入以下内容:
int userinput = 0;
// Keep on asking until valid input provided - an integer number in [1..10] range
while (true) {
Console.WriteLine("Enter a number between 1 and 10");
if (!int.TryParse(Console.ReadLine(), out userinput))
Console.WriteLine("Sorry, syntax error; try again");
else if (userinput < 0)
Console.WriteLine("Your input less than 0; try again");
else if (userinput > 10)
Console.WriteLine("Your input greater than 10; try again");
else
break; // <- Valid input
}
答案 1 :(得分:0)
您输入的Console.ReadLine();
超出了您的需求
通过不对代码进行更多更改
// Boolean if user passes
Boolean Userpassed;
// Declares Answer Variable
int answer = 6;
// Asks user for a number
Console.WriteLine("Enter a number between 1 and 10");
// Displays Message
// Console.ReadLine(); <== Remove this line
// Takes User Input as String
string useranswer = Console.ReadLine();
// Declares integer for actual integer
int userinput;
// User string parsed to Int
userinput = Convert.ToInt32(useranswer); //<== pass useranswer to here
// Displays Message
// Console.ReadLine(); <== Remove this line
// Checks if input is equal to answer
//your rest code is same here
答案 2 :(得分:0)
别担心,你会学到这些东西
int ans = 6;
Console.WriteLine("Enter a number between 1 and 10");
//taking integer input from user
int input = Convert.ToInt32(Console.ReadLine());
//now checking user input with selected answere
if(ans == input)//true, when user predicts correct number
{
Console.WriteLine("Congratulations!!!");
}
else if(input < ans)//true, when user predicted number < actual number
{
Console.WriteLine("The actual answer is less than what you entered");
}
else//true, when both above conditions are false
{
Console.WriteLine("The actual answer is greater than what you entered");
}