[英]How to make a single input satisfy 2 scanners at the same time
So I'm making a calculator app, and I'm stucked in this one, I'm wondering if there's a way to make a single input from the user for two scanners at the same time.所以我正在制作一个计算器应用程序,我被困在这个应用程序中,我想知道是否有办法让用户同时为两个扫描仪进行单一输入。 Because I'm planning to create a Double ( scanner.nextDouble() ) variable while also checking if it's true or false that it is containing a number ( scanner.hasNextDouble() ).因为我打算创建一个Double ( scanner.nextDouble() )变量,同时还要检查它是否包含一个数字( scanner.hasNextDouble() )是真还是假。
btw, I'm a beginner so I'm sorry if my question is hard to understand.顺便说一句,我是初学者,所以如果我的问题难以理解,我很抱歉。
import java.util.Scanner;
public class Actions {
static Scanner scanner = new Scanner(System.in);
private final double x;
private final double y;
private final int operation;
Actions() {
this.x = getDoubleX();
this.y = getDoubleY();
this.operation = getOperation();
showAnswer();
}
public static double getDoubleX() {
double temp = 0;
String temp1;
boolean temp2 = true;
System.out.println("input your first number: ");
while (temp2) {
/*
I tried a solution like this. but it didn't work out.
*/
temp1 = scanner.nextLine();
temp = Double.parseDouble(temp1);
if (Double.isNaN(temp)) {
System.out.println("try again! ");
} else {
temp2 = false;
}
}
return temp;
}
public static double getDoubleY() {
double temp = 0;
boolean temp1 = true;
boolean temp2 = false; //I'm talking about this
System.out.println("input your second number: ");
while (temp1) {
temp = scanner.nextDouble();
if (!Double.isNaN(temp)){
temp1=false;
} else {
System.out.println("try again!");
}
}
return temp;
}
public static int getOperation() {
int temp = 0;
boolean temp1 = true;
System.out.println("input your desired operation, \n" +
"1 = addition, 2 = subtraction, 3 = multiplication, 4 = division");
while (temp1) {
temp = scanner.nextInt();
if (temp >= 1 && temp <= 4) {
temp1 = false;
} else {
System.out.println("try again");
}
}
return temp;
}
public void showAnswer() {
if (x % 1 == 0 & y % 1 == 0) {
if (operation == 1) {
System.out.println("the sum is: " + Math.round(x + y));
} else if (operation == 2) {
System.out.println("the difference is: " + Math.round(x - y));
} else if (operation == 3) {
System.out.println("the product is: " + Math.round(x * y));
} else {
System.out.println("the quotient is: " + Math.round(x / y));
}
} else {
if (operation == 1) {
System.out.println("the sum is: " + (x + y));
} else if (operation == 2) {
System.out.println("the difference is: " + (x - y));
} else if (operation == 3) {
System.out.println("the product is: " + (x * y));
} else {
System.out.println("the quotient is: " + (x / y));
}
}
}
} }
Don't use two Scanners.不要使用两个扫描仪。 It isn't necessary.这是没有必要的。 Try the following (which you may need to adjust for your specific application).尝试以下操作(您可能需要针对您的特定应用进行调整)。
Scanner scan = new Scanner(System.in);
double d = 0;
while (true) {
System.out.print("Enter a double value: ");
String possibleDouble = scan.next();
try {
d = Double.parseDouble(possibleDouble);
break;
} catch (NumberFormatException ne) {
System.out.printf("bad input (%s) - try again!%n",
possibleDouble);
}
}
System.out.println(d);
You could put the above in a method with specific args to validate input.您可以将上述内容放在具有特定参数的方法中以验证输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.