简体   繁体   English

C#If(textbox.text = number)错误

[英]C# If(textbox.text=number) error

I making a quiz in which users would have to enter a number (eg 4) into a TextBox then the program would check if the number entered is correct. 我进行了一个测验,其中用户必须在TextBox输入数字(例如4),然后程序将检查输入的数字是否正确。 Unfortunately, I'm having some trouble with this part of the code. 不幸的是,我在这部分代码上遇到了麻烦。

So currently I have this code: 所以目前我有这段代码:

if(textbox1.Text=4)

but the 4 is underlined with the error message: 但是错误消息用下划线标出4

cannot implicitly convert type 'int' to 'string'. 无法将类型'int'隐式转换为'string'。

Can I trouble you all to help me find out what's wrong with my code? 我可以麻烦大家帮助我找出我的代码有什么问题吗? Thank you so much!! 非常感谢!!

Since textbox1.Text is of type string , you have to parse : 由于textbox1.Textstring类型,因此您必须解析

   int answer;

   //  TryParse - the value entered can be treated as a valid integer 
   //  answer == correctAnswer - the answer provided is a correct one 
   if (int.TryParse(textbox1.Text, out answer) && answer == correctAnswer) {
     ...
   }

Please, notice that the implementation tolerates leading and traling spaces (typical problem in quizes): if user happens to input "4 " (trailing space) the answer will be accepted providing that correctAnswer == 4 请注意,该实现可以容忍前导后继空格(测验中的典型问题):如果用户碰巧输入了"4 "后继空格),则可以接受答案, correctAnswer == 4correctAnswer == 4

if(textbox1.Text == Convert.ToString(4))

要么

if(textbox1.Text == "4")

You are trying to compare a string with an int . 您正在尝试将stringint进行比较。

You need to use if(textbox1.text == "4") 您需要使用if(textbox1.text == "4")

also note the double == for comparisons 还要注意双==进行比较

您需要解析为int

if(Int32.Parse(textbox1.Text) == 4)

You are comparing a string (textbox1.Text) with an integer (4). 您正在将string (textbox1.Text)与integer (4)进行比较。 To make this work you have to compare same data types. 为了使这项工作,您必须比较相同的数据类型。 Some options: 一些选项:

if(textbox1.Text == "4") 

or 要么

if(textbox1.Text == 4.ToString())

or 要么

if(int.Parse(textbox1.Text) == 4)

NOTE: In the last option you can get an exception if the text in the textbox is not a number. 注意:在最后一个选项中,如果文本textbox不是数字,则会出现异常。 So if you want to convert to integer than I would suggest: 因此,如果要转换为整数,则建议不要:

int guessedNumber; 
Int32.TryParse(textbox1.Text, out guessedNumber);
if(guessedNumber == 4)

The Text property is of type string and 4 is an int so the comparison results in a compile time error. Text属性的类型为string而4为int类型,因此比较会导致编译时错误。

Use the following code to perform the check. 使用以下代码执行检查。

if (int.Parse(textbox1.Text) == 4)
{
    // do something
}

If you're unsure whether the user is going to provide the input correctly or if you haven't set any validations on the model, then you should parse the input and then check whether the user entered 4. Here's the rextester link 如果不确定用户是要正确提供输入还是没有对模型进行任何验证,则应解析输入,然后检查用户是否输入了4。这是rextester 链接

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var input = "hello 4";
            // var input = "4";
            int number;
            var isNumber = int.TryParse(input, out number);
            if (isNumber)
            {
                if (number == 4)
                {
                    Console.WriteLine("The Number is 4");
                }
                else
                {
                    Console.WriteLine("The Number isn't 4");
                }
            }
            else
            {
                Console.WriteLine("Not a valid number");
            }
        }
    }
}
if(textbox1.Text == "4")
{
//Do Something
}

One way or another, you have to make sure both values are to make sure you are comparing two values "==" (not "=", unless u want to change the value) and that both values are same data type 一种或另一种方式,您必须确保两个值都确保要比较两个值“ ==”(不是“ =”,除非您要更改值),并且两个值都是相同的数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM