操作员'&燃气轮机';和'<';无法应用于类型为';字符串';和';字符串';C#

操作员'&燃气轮机';和'<';无法应用于类型为';字符串';和';字符串';C#,c#,operator-keyword,C#,Operator Keyword,Visual Studio 2015中制作的C#程序,要求用户猜测1-10之间的数字,告诉用户猜测是否正确,大于或小于必须猜测的值 static void Main(string[] args) { string rightGuess = "7"; Console.WriteLine("Guess the right number from 1-10: "); string userGuess; userGuess = C

Visual Studio 2015中制作的C#程序,要求用户猜测1-10之间的数字,告诉用户猜测是否正确,大于或小于必须猜测的值

static void Main(string[] args)
    {
        string rightGuess = "7";

        Console.WriteLine("Guess the right number from 1-10: ");
        string userGuess;
        userGuess = Console.ReadLine();
        {
            if (userGuess == rightGuess)
                Console.WriteLine("You guessed right!");
            else if (userGuess > rightGuess)
                Console.WriteLine("Wrong guess. Your guess was greater than the right guess.");
            else (userGuess < rightGuess)
                Console.WriteLine("Wrong guess. Your guess was lesser than the right guess.");
        }
    }
static void Main(字符串[]args)
{
字符串rightGuess=“7”;
WriteLine(“从1-10中猜出正确的数字:”;
字符串用户猜测;
userGuess=Console.ReadLine();
{
if(userGuess==rightGuess)
控制台。WriteLine(“你猜对了!”);
else if(userGuess>rightGuess)
WriteLine(“猜错了。你的猜测比正确的猜测大。”);
else(userGuess
程序在Visual Studio 2015中返回以下错误:


我在谷歌上研究了大约一个小时如何解决错误,但没有一个解决方案修复了错误

您需要比较整数而不是字符串(要实现这种比较),请将此行更改为:

int rightGuess = 7;

int userGuess = int.Parse(Console.ReadLine());
它会起作用的。当然,您可以添加
int.TryParse
并检查输入是否实际上是一个int

int userGuess;

if(int.TryParse(Console.ReadLine(), out userGuess))
{
    ... do your logic
}
else
{
    Console.WriteLine("Not a number");
}

您需要比较整数而不是字符串(要实现这种比较),请将此行更改为:

int rightGuess = 7;

int userGuess = int.Parse(Console.ReadLine());
它会起作用的。当然,您可以添加
int.TryParse
并检查输入是否实际上是一个int

int userGuess;

if(int.TryParse(Console.ReadLine(), out userGuess))
{
    ... do your logic
}
else
{
    Console.WriteLine("Not a number");
}

使用int.Parse/TryParse或使用string.CompareTo()将字符串转换为数值。

使用int.Parse/TryParse或使用string.CompareTo()将字符串转换为数值。

您应该使用正确的数据类型进行比较

int rightGuess = 7;
Console.WriteLine("Guess the right number from 1-10: ");
int userGuess;
userGuess = int.Parse(Console.ReadLine());
{
    if (userGuess == rightGuess)
        Console.WriteLine("You guessed right!");
    else if (userGuess > rightGuess)
        Console.WriteLine("Wrong guess. Your guess was greater than the right guess.");
    else (userGuess < rightGuess)
        Console.WriteLine("Wrong guess. Your guess was lesser than the right guess.");
}
int rightGuess=7;
WriteLine(“从1-10中猜出正确的数字:”;
int-userGuess;
userGuess=int.Parse(Console.ReadLine());
{
if(userGuess==rightGuess)
控制台。WriteLine(“你猜对了!”);
else if(userGuess>rightGuess)
WriteLine(“猜错了。你的猜测比正确的猜测大。”);
else(userGuess
当你说“Mohit”大于“Mikex64”时,你会这样想,这有什么意义吗

但是2大于1是有意义的。因此,我们可以像2>1那样编写它,但不能编写“Mohit”>“Mikex64”,因此您会收到此错误消息


编辑:编辑代码中的“大于”和“小于”操作数,使之准确,因为我第一次写错了它们。

您应该使用正确的数据类型进行比较

int rightGuess = 7;
Console.WriteLine("Guess the right number from 1-10: ");
int userGuess;
userGuess = int.Parse(Console.ReadLine());
{
    if (userGuess == rightGuess)
        Console.WriteLine("You guessed right!");
    else if (userGuess > rightGuess)
        Console.WriteLine("Wrong guess. Your guess was greater than the right guess.");
    else (userGuess < rightGuess)
        Console.WriteLine("Wrong guess. Your guess was lesser than the right guess.");
}
int rightGuess=7;
WriteLine(“从1-10中猜出正确的数字:”;
int-userGuess;
userGuess=int.Parse(Console.ReadLine());
{
if(userGuess==rightGuess)
控制台。WriteLine(“你猜对了!”);
else if(userGuess>rightGuess)
WriteLine(“猜错了。你的猜测比正确的猜测大。”);
else(userGuess
当你说“Mohit”大于“Mikex64”时,你会这样想,这有什么意义吗

但是2大于1是有意义的。因此,我们可以像2>1那样编写它,但不能编写“Mohit”>“Mikex64”,因此您会收到此错误消息


编辑:编辑代码中的“大于”和“小于”操作数,使其准确无误,因为我第一次写错了它们。

您正在尝试比较字符串。首先,使用类似于
int.TryParse
的方法将用户输入转换为整数。您正在尝试比较字符串。首先,使用类似于
int.TryParse
的方法将用户输入转换为整数。感谢您提供此解决方案。它解决了这个问题。我不知道两者都必须是int而不是string。@Mikex64很乐意帮助:)感谢您提供此解决方案。它解决了这个问题。我不知道两者都必须是int而不是string。@Mikex64很乐意帮助:)string.Compare在这种情况下不起作用:“17”是<“7”例如string.Compare在这种情况下不起作用:“17”是<“7”例如。现在我理解了string和int之间的区别。我没有意识到string是文本而不是数字,而int只是数字。虽然“Mohit”>“Mikex64”在这种情况下会起作用,因为你在编程方面比我强;)谢谢非常感谢您如此慷慨友好的评论:)“Mohit”=“Mikex64”现在我理解了字符串和int之间的区别。我没有意识到字符串是文本而不是数字,而int只是数字。虽然“Mohit”>“Mikex64”在这种情况下会起作用,因为你在编程方面比我强;)谢谢非常感谢您如此慷慨友好的评论:)“Mohit”=“Mikex64”