如何读取一个输入行中的某个变量? (爪哇)
如何在 Java 的一个输入行中读取某个变量?
抱歉我的英语初学者
例如: 输入 a、b、c:1.0 3 1
然后我想要a=1.0,b=3和c=1。
如何用 Java(和 C#)实现?
我尝试过这种格式,但是当输入数字是实数时它不起作用
Scanner sc = new Scanner(System.in).useDelimiter(" ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
sc.close()
How can I read some variable in one inputline in Java?
Sorry for my beginner english
Forexample:Enter a, b, c: 1.0 3 1 <Enter>
Then I would like to the a=1.0, b=3 and c=1.
How can I implement in Java (and C#)?
I tried in this format, but It doesn't work when the input number is an real number
Scanner sc = new Scanner(System.in).useDelimiter(" ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
sc.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的解决方案非常好。最好用“;”作为分隔符而不是“”,这样会更清楚。如果我想输入像 3.5 这样的数字,我必须使用“,”而不是“.”。我认为你必须在最后一个数字之后再次使用分隔符。
这是我对你的代码的测试:
输入:
1,3;4;5;
输出:
--> 1.3
--> 4.0
--> 5.0
i think your solution is pretty good. better take ";" as a delimiter instead of " ", that will make it more clear. if I want to type a number like 3.5, i have to use "," instead of ".". and i think you have to use the delimiter after your last number again.
this is my test with your code:
input:
1,3;4;5;
output:
--> 1.3
--> 4.0
--> 5.0
这将读取由空格或换行符分隔的数字。只需换行或按 Enter 键即可终止扫描。
示例运行
编辑
您可以将数字输入到单独的变量中:
示例运行
This will read numbers seperated by space or newlines. Just a newline or hitting enter will terminate scanning.
Sample Run
EDIT
You could input the numbers into seperate variables:
Sample Run