接受以空格分隔的双精度类型输入,在Java中实现

huangapple go评论76阅读模式
英文:

Taking input type double separated by space in Java

问题

我需要使用扫描器(Scanner)从用户那里接收以空格分隔的双精度(double)数据类型的输入。

Scanner input = new Scanner(System.in);
System.out.print("exampleDouble: ");
double db = input.nextDouble();
db += input.nextLine();

我认为这个代码片段应该可以工作,寻找一个简单的语句来捕获两个值(都是双精度,用空格分隔)。不使用数组。只需要捕获两个值,不多不少。
示例:58.0 57.3

英文:

I need to use scannner to take input from user for data type double separated by white space.

 Scanner input = new Scanner(System.in);
	        System.out.print("exampleDouble: ");
	        double db = input.nextDouble();
	        db+=input.nextLine();

I thought this would work, looking for a simple statement for capturing 2 values (both double, separated by space). no arrays. only need to capture 2 values , not more.
Example : 58.0 57.3

答案1

得分: 1

**不使用数组**

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double double1 = input.nextDouble();
        double double2 = input.nextDouble();
        System.out.println("Double1 is " + double1);
        System.out.println("Double2 is " + double2);
    }
}

**使用数组**

你可以将输入分割为一个字符串数组然后使用 `Double.parseDouble` 进行解析如下所示

Scanner input = new Scanner();
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);

因此完整的代码如下

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        String[] userinput = input.nextLine().split(" ");
        double double1 = Double.parseDouble(userinput[0]);
        double double2 = Double.parseDouble(userinput[1]);
        System.out.println("Double1 is " + double1);
        System.out.println("Double2 is " + double2);
    }
}

**示例输入/输出**

*输入*

2.5 3.5

*输出*

Double1 is 2.5
Double2 is 3.5

**然而**正如 @Pschemo 所指出的你应该只使用两次 `input.nextDouble()`。
英文:

WITHOUT USING ARRAYS

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double double1 = input.nextDouble();
double double2 = input.nextDouble();
System.out.println("Double1 is "+double1);
System.out.println("Double2 is "+double2);
}
}

WITH ARRAYS

You can split it into a String array, and then parse them using Double.parseDouble like so:

Scanner input = new Scanner();
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);

Therefore, the full code would be:

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);
System.out.println("Double1 is "+double1);
System.out.println("Double2 is "+double2);
}
}

Sample I/O

Input

2.5 3.5

Output

Double1 is 2.5
Double2 is 3.5

HOWEVER: As @Pschemo so aptly pointed out - you should just be using input.nextDouble() twice.

huangapple
  • 本文由 发表于 2020年10月3日 10:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64180160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定