如何读取一个输入行中的某个变量? (爪哇)

发布于 2024-12-05 13:21:51 字数 393 浏览 0 评论 0原文

如何在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2024-12-12 13:21:51

我认为你的解决方案非常好。最好用“;”作为分隔符而不是“”,这样会更清楚。如果我想输入像 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

送君千里 2024-12-12 13:21:51

这将读取由空格或换行符分隔的数字。只需换行或按 Enter 键即可终止扫描。

Scanner sc = new Scanner(System.in);
Pattern newlineOrSpace = Pattern.compile(System
        .getProperty("line.separator") + "|\\s");
sc.useDelimiter(newLineOrSpace);
List<Double> numbers = new ArrayList<Double>();
System.out.println("Enter numbers:");
while (sc.hasNextDouble()) {
    numbers.add(sc.nextDouble());
}
for (Double number : numbers) {
    System.out.println(number);
}

示例运行

Enter numbers:
1 2 3
5.5 7.2
.1

1.0
2.0
3.0
5.5
7.2
0.1

编辑

您可以将数字输入到单独的变量中:

Scanner sc = new Scanner(System.in);
Pattern newlineOrSpace = Pattern.compile(System
        .getProperty("line.separator") + "|\\s");
sc.useDelimiter(newlineOrSpace);
System.out.print("Enter a, b, c: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
System.out.format("a = %f, b = %f, c = %f", a, b, c);

示例运行

Enter a, b, c: 1.0 5 9
a = 1.000000, b = 5.000000, c = 9.000000

This will read numbers seperated by space or newlines. Just a newline or hitting enter will terminate scanning.

Scanner sc = new Scanner(System.in);
Pattern newlineOrSpace = Pattern.compile(System
        .getProperty("line.separator") + "|\\s");
sc.useDelimiter(newLineOrSpace);
List<Double> numbers = new ArrayList<Double>();
System.out.println("Enter numbers:");
while (sc.hasNextDouble()) {
    numbers.add(sc.nextDouble());
}
for (Double number : numbers) {
    System.out.println(number);
}

Sample Run

Enter numbers:
1 2 3
5.5 7.2
.1

1.0
2.0
3.0
5.5
7.2
0.1

EDIT

You could input the numbers into seperate variables:

Scanner sc = new Scanner(System.in);
Pattern newlineOrSpace = Pattern.compile(System
        .getProperty("line.separator") + "|\\s");
sc.useDelimiter(newlineOrSpace);
System.out.print("Enter a, b, c: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
System.out.format("a = %f, b = %f, c = %f", a, b, c);

Sample Run

Enter a, b, c: 1.0 5 9
a = 1.000000, b = 5.000000, c = 9.000000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文