为什么我不能将变量'choice'作为int类型? [英] Why cannot I make the variable 'choice' as int type in this ?

查看:158
本文介绍了为什么我不能将变量'choice'作为int类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能将变量''choice''作为int类型?我尝试将选择作为int类型,但它给出了错误,我无法隐式地将字符串转换为int。



Why cannot I make the variable ''choice'' as int type in this ? I tried making choice as int type but it gave the error that I cannot convert string to int implicitly.

Console.WriteLine("Enter a choice");
string choice;
choice = Console.ReadLine();
Console.WriteLine("you have entered:" + choice);
Console.ReadLine();

推荐答案

Console.ReadLine [ ^ ]返回一个字符串,因此您无法使用它来读取整数。您需要使用 Int32 <之一 [ ^ ] Parse 转换字符串的方法。
Console.ReadLine[^] returns a string, so you cannot use it to read an integer. You need to use one of the Int32[^] Parse methods to convert the string.


Console.ReadLine()

返回类型是字符串所以选择必须是字符串,但是如果你想将 choice 声明为int而不是按照下面的代码



return type is string so choice must be string, but if you want to declare choice as int than follow the code below

Console.WriteLine("Enter a choice");
int choice;
if (int.TryParse(Console.ReadLine(), out choice))
{
    Console.WriteLine("you have entered:" + choice);
}
else
{
    Console.WriteLine("you have entered non numberic number" );
}
Console.ReadLine();


Hii professional



ya Richard是对的........

没有任何选择错误。

主要的错误是你要读取int数据类型到字符串直接没有任何转换'为什么'不能隐含地将字符串转换为int即将来临...



更多知识阅读此链接..... ....



http://www.dotnetperls.com/内部解析 [ ^ ]



乐于帮助......
Hii professional

ya Richard is right........
there is no any error with choice.
the main fault is that you want to read int data type to string directly without any conversion that''s why "cannot convert string to int implicitly" is coming...

for more knowledge read this link.........

http://www.dotnetperls.com/int-parse[^]

Happy to help...


这篇关于为什么我不能将变量'choice'作为int类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆