英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论