繁体   English   中英

如何使单个输入同时满足 2 个扫描仪

[英]How to make a single input satisfy 2 scanners at the same time

所以我正在制作一个计算器应用程序,我被困在这个应用程序中,我想知道是否有办法让用户同时为两个扫描仪进行单一输入。 因为我打算创建一个Doublescanner.nextDouble() )变量,同时还要检查它是否包含一个数字( scanner.hasNextDouble() )是还是

顺便说一句,我是初学者,所以如果我的问题难以理解,我很抱歉。


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));
        }
    }
}

}

不要使用两个扫描仪。 这是没有必要的。 尝试以下操作(您可能需要针对您的特定应用进行调整)。

  • 这将继续提示,直到输入有效的双精度。
  • 然后它将跳出while循环。
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);

您可以将上述内容放在具有特定参数的方法中以验证输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM