阅读从控制台的用户输入 [英] Read user input from console

查看:146
本文介绍了阅读从控制台的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了一些来自用户则多采用PI数字的号码。我尝试了喜欢的东西,如下。但 A 包含乱码。例如,如果我插入 22 ,一个包含 50

 双A,B;
一个= Console.Read();
B = A * Math.PI;
Console.WriteLine(B);
 

解决方案

我不知道你的问题是什么(因为你还没有告诉我们的),但我猜测

  A = Console.Read();
 

这只会从控制台读取字符。

您可以改变你的程序了这一点。为了使其更加坚固,接受超过1个字符的输入,并验证输入实际上是一个数字:

 双A,B;
Console.WriteLine(istenensayıyısonuna .00 koyarak YAZ);
如果(double.TryParse(到Console.ReadLine(),走出了一条)){
  B = A * Math.PI;
  Console.WriteLine(Sonuç+ B);
} 其他 {
  //用户给了一个非法的输入。在这里处理。
}
 

I want a number from user then multiple with Pi number that to the number. I tried a thing like as below. But a contains gibberish. For example if I insert 22, a contains 50.

double a,b;
a = Console.Read();
b = a * Math.PI;
Console.WriteLine(b);

解决方案

I'm not sure what your problem is (since you haven't told us), but I'm guessing at

a = Console.Read();

This will only read on char from your Console.

You can change your program to this. To make it more robust, accept more than 1 char input, and validate that the input is actually a number:

double a, b;
Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz");
if (double.TryParse(Console.ReadLine(), out a)) {
  b = a * Math.PI;
  Console.WriteLine("Sonuç " + b); 
} else {
  //user gave an illegal input. Handle it here.
}

这篇关于阅读从控制台的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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