Zhi*_*Teh 1 c# textbox if-statement implicit-conversion
我做了一个测验,其中用户必须输入一个数字(例如4),TextBox然后程序将检查输入的数字是否正确.不幸的是,我在使用这部分代码时遇到了一些麻烦.
所以目前我有这个代码:
if(textbox1.Text=4)
Run Code Online (Sandbox Code Playgroud)
但是4带有错误消息的下划线:
不能隐式地将类型'int'转换为'string'.
我可以麻烦大家帮我找出我的代码有什么问题吗?非常感谢!!
由于textbox1.Text是类型string,你必须解析:
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) {
...
}
Run Code Online (Sandbox Code Playgroud)
请注意,实现允许前导空格和traling空间(quizes中的典型问题):如果用户碰巧输入"4 "(尾随空格),将接受答案,correctAnswer == 4
| 归档时间: |
|
| 查看次数: |
1211 次 |
| 最近记录: |