怎样接收键盘输入阿?

nuli2000 2001-05-28 07:26:00
我想从键盘接收一个int型参数,我是这样写的:
using system;
class myclass
{
static void Main()
{
int x=Console.ReadLine();
}
}
但是编译器说我 Can't not convert string to int。
我刚学C#,问个笑话的问题。不过照样给分。
...全文
155 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
benwg 2001-06-01
  • 打赏
  • 举报
回复
还是使用ReadLine吧.
static void Main() {
int x=0;
int y=0;
Console.WriteLine("Now, please input the x value: ");
x=Console.ReadLine().ToInt32();
Console.WriteLine("x={0}",x);
Console.WriteLine("Now, please input the y value: ");
y=Console.ReadLine().ToInt32();
Console.WriteLine("y={0}",y);
Console.ReadLine();
}
AWP365 2001-06-01
  • 打赏
  • 举报
回复
对,ReadLine()能解决这个问题,只是每次要Enter,好处是可以输入多个字符
AWP365 2001-06-01
  • 打赏
  • 举报
回复
因为第7行已经接受一个字符了,那就是回车!!在第四行时不要俺会车
bluestar 2001-05-31
  • 打赏
  • 举报
回复
readline返回的是一个string型,如果是Console.Read()返回一个int型。

那个不暂停的问题我想是一个bug
nuli2000 2001-05-30
  • 打赏
  • 举报
回复
我试过,对了!!!!!
但是我还有个问题:
如下代码:
1: int x=0;
2: int y=0;
3: Console.WriteLine("Now, please input the x value: ");
4: x=Console.Read();
5: Console.WriteLine("x={0}",x);
6: Console.WriteLine("Now, please input the y value: ");
7: y=Console.Read();
为什么执行到第4行的时候,会停下来让我输入x的值,但是输入一个数字后,按回车,第
7行却不能停下来呢?Optimus如果知道为什么的话,我就把分给你了!!!
AWP365 2001-05-30
  • 打赏
  • 举报
回复
Optimus说的对,我用过,就那样
Optimus 2001-05-28
  • 打赏
  • 举报
回复
要用ToInt32(),或者ToInt16(),ToInt64(),就看你的OS平台了:〉
efoxxx 2001-05-28
  • 打赏
  • 举报
回复
Sorry,刚才少了个括号:int x=Console.ReadLine().ToInt();
efoxxx 2001-05-28
  • 打赏
  • 举报
回复
int x=Console.ReadLine().ToInt;你试一试,
我也是蒙的,因为我这里没有装成功C#,如果对的话,给我个mail(chgm@cad.wox.org)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧