我正在尝试学习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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jus*_*ner 24
if(myInt = 10)
Run Code Online (Sandbox Code Playgroud)
分配值10 myInt而不是检查是否相等.它应该成为:
if(myInt == 10)
Run Code Online (Sandbox Code Playgroud)
这是测试相等性的正确语法.
kem*_*002 12
这个:
if (myInt = 10)
Run Code Online (Sandbox Code Playgroud)
需要是这样的:
if (myInt == 10)
Run Code Online (Sandbox Code Playgroud)
或者也可能是这样:
if(myInt.Equals(10))
Run Code Online (Sandbox Code Playgroud)
我知道这可能只是一个错字,但我想我还是会包含这个链接:
http://msdn.microsoft.com/en-us/library/53k8ybth.aspx
这是Equals函数的链接:
http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx
其实
这个:也myInt = Int32.Parse(myInput);
应该是这样的
int myInt;
if(Int32.TryParse(myInput, out myInt))
{
rest of code.
}
else
{
Console.WriteLine("You didn't provide a number");
}
Run Code Online (Sandbox Code Playgroud)
以防提供的输入不是数字.
http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx
| 归档时间: |
|
| 查看次数: |
474 次 |
| 最近记录: |