简体   繁体   English

C#初学者问题

[英]c# beginner problem

I am trying to learn C# and I have a problem with the following code: 我正在尝试学习C#,以下代码有问题:

using System;

class IfSelect
{
    public static void Main()
    {
        string myInput;
        int myInt;
        Console.Write("Please enter a number: ");
        myInput = Console.ReadLine();
        myInt = Int32.Parse(myInput);

        if (myInt = 10)
        {
            Console.WriteLine("Your number is 10.", myInt);
        }
    }
}
if(myInt = 10)

Assigns the value of 10 to myInt instead of checking for equality. 将10的值分配给myInt而不是检查是否相等。 It should become: 它应该变成:

if(myInt == 10)

which is the correct syntax for testing equality. 这是测试相等性的正确语法。

This: 这个:

if (myInt = 10)

needs to be this: 需要这样的:

if (myInt == 10)

or it could be this too: 或者也可能是这样:

if(myInt.Equals(10))

I know it's probably just a typo but, I thought I would include the link anyway: 我知道这可能只是拼写错误,但我想无论如何我都会包含链接:

http://msdn.microsoft.com/en-us/library/53k8ybth.aspx http://msdn.microsoft.com/zh-CN/library/53k8ybth.aspx

here is a link to the Equals function: 这是Equals函数的链接:

http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx http://msdn.microsoft.com/zh-CN/library/ms173147(VS.80).aspx

Actually 其实

this: myInt = Int32.Parse(myInput); 这: myInt = Int32.Parse(myInput); should probably be something like this also 也应该是这样的

int myInt;

if(Int32.TryParse(myInput, out myInt))
{
  rest of code.
}
else
{
  Console.WriteLine("You didn't provide a number");
}

Just in case the provided input isn't a number. 以防万一提供的输入不是数字。

http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx http://msdn.microsoft.com/zh-CN/library/system.int32.tryparse.aspx

Instead of if (myInt = 10) you need to use if (myInt == 10) . 代替if (myInt = 10)您需要使用if (myInt == 10)

The first is the assignment operator, the second is a comparison operator. 第一个是赋值运算符,第二个是比较运算符。

Your if statement is assigning 10 to myInt rather than testing for equality. 您的if语句将10分配给myInt而不是进行相等性测试。

Do this: 做这个:

if(myInt == 10)

Change 更改

if (myInt = 10)

to

if (myInt == 10)

Offhand, you're using the assignment operator, not the equality operator. 暂时,您使用的是赋值运算符,而不是相等运算符。 if (myInt = 10) should probably be if (myInt == 10) . if (myInt = 10)应该应该是if (myInt == 10)

Also, in 另外,在

Console.WriteLine("Your number is 10.", myInt);

the myInt parameter is pointless. myInt参数毫无意义。 Should either use 应该使用

Console.WriteLine("Your number is 10.");

or 要么

Console.WriteLine("Your number is {0}.", myInt);
  if (myInt = 10)
  {
   Console.WriteLine("Your number is 10.", myInt);
  } 

Should change = to == to check for equality 应该将=更改为==以检查是否相等

You can try this, where you use StreamReader to capture input via the console. 您可以尝试使用StreamReader通过控制台捕获输入的方法。

Secondly you need the == (which means "Equals") instead of (=) that is an assignment operator a common mistake when you first start programming. 其次,您需要==(表示“等于”)而不是(=),这是赋值运算符,这是您首次开始编程时的常见错误。

This however wont work as my code will have an error, but this just gives an example of where your mistake is. 但是,这将不起作用,因为我的代码将出现错误,但这仅举一个示例说明您的错误所在。

class IfSelect
{
    public static void Main()
    {
            string myInput;
            int myInt;

            StreamReader reader = new StreamReader();

            Console.Write("Please enter a number: ");
            myInput = reader.ReadLine();
            myInt = Int32.Parse(myInput);

            if (myInt == 10)
            {
                Console.WriteLine("Your number is 10.", myInt);
            }
        }

    }

I think there's three problems: 我认为存在三个问题:

  1. It doesn't compile. 它不会编译。 (myInt = 10) doesn't assign to myInt - it just wont compile. (myInt = 10)不会分配给myInt-它只会编译。
  2. If it did, 如果有的话

    Console.WriteLine("Your number is 10.", myInt); Console.WriteLine(“您的数字是10。”,myInt);

    Will always display 10. 始终显示10。

  3. You won't be able to see the result. 您将看不到结果。

Try the following: 请尝试以下操作:

class IfSelect
{
public static void Main()
{
    string myInput;
    int myInt;
    Console.Write("Please enter a number: ");
    myInput = Console.ReadLine();
    myInt = Int32.Parse(myInput);

    if (myInt == 10)
    {
        Console.WriteLine(string.Format("Your number is {0}.  Press any key.", myInt));
        Console.ReadLine();
    }
}
}

In addition to using == instead of = it's also a good practice to put the value before the variable like 除了使用==代替=外,将值放在变量之前也是一种好习惯,例如

if(10 == myInt)

So the compiler will catch when you only put 1 equal sign by accident. 因此,当您偶然仅放1个等号时,编译器将捕获。

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

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