gpt4 book ai didi

java - 使用 Java 计算器创建无效输入的错误消息

转载 作者:行者123 更新时间:2023-12-01 11:07:41 26 4
gpt4 key购买 nike

我必须用 Java 创建一个计算器。到目前为止一切正常,除了我不太清楚当有人在要求输入数字时输入的值不是整数时如何产生错误消息。我尝试了“try, catch”语句,但它仍然在 IDE 中抛出一个错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)

这是我的程序其余部分的代码:

import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {

static int num1, num2;
static int memory;
static String operation;
static String menu = "\nChoose an operation:\n+ Add\n- Subtract\n* Multiply\n/ Divide\n^ Exponent\n~ Square Root\n Exit";
static boolean run = true;

public static void menu (String menu){
System.out.println (menu);
};

public static void add (int num1, int num2) {
System.out.println (num1 +num2);}

public static void subtract (int num1, int num2) {
System.out.println (num1 - num2);
};

public static void multiply (int num1, int num2) {
System.out.println (num1 * num2);
};

public static void divide (int num1, int num2) {
if (num2 !=0) {
System.out.println (num1/num2);}
else{
System.out.println ("Cannot divide by 0");
};
};

public static void exp (int num1, int num2) {
System.out.println (Math.pow(num1,num2));
};

public static void sqrt (int num1) {
System.out.println (Math.sqrt(num1));
};

public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);

do{
menu(menu);
operation = scanner.next();

try {
switch (operation) {
case "+":
System.out.println ("Enter first number:");
num1 = scanner.nextInt();
System.out.println ("Please enter a valid number");
System.out.println ("Enter second number:");
num2 = scanner.nextInt();
add (num1,num2);
break;
case "-":
System.out.println ("Enter first number:");
num1 = scanner.nextInt();
System.out.println ("Enter second number:");
num2 = scanner.nextInt();
subtract (num1, num2);
break;
case "*":
System.out.println ("Enter first number:");
num1 = scanner.nextInt();
System.out.println ("Enter second number:");
num2 = scanner.nextInt();
multiply (num1, num2);
break;
case "/":
System.out.println ("Enter first number:");
num1 = scanner.nextInt();
System.out.println ("Enter second number:");
num2 = scanner.nextInt();
divide (num1, num2);
break;
case "^":
System.out.println ("Enter first number:");
num1 = scanner.nextInt();
System.out.println ("Enter exponent:");
num2 = scanner.nextInt();
exp (num1, num2);
break;
case "~":
System.out.println ("Enter number:");
num1 = scanner.nextInt();
sqrt(num1);
break;
case "Exit":
System.out.println("You have exited the calculator");
System.exit(0);
run = false;
break;
default:
System.out.println("Invalid");
}
}catch (InputMismatchException e) {

}
}while(run == true);



scanner.close();
}

};

谢谢!

好的更新:我得到了 try catch 来抛出错误,但是当我运行它时出现错误,它运行 switch 语句的默认情况,然后再次运行(所以你会看到菜单两次),我只需要它出现一次

最佳答案

case "+":
System.out.println ("Enter first number:");
while(!scanner.hasNextInt())
{
System.err.println("Enter valid int");
scanner.next();
}

num1 = scanner.nextInt();
System.out.println ("Enter second number:");
while(!scanner.hasNextInt())
{
System.err.println("Enter valid int");
scanner.next();
}

num2 = scanner.nextInt();
add(num1, num2);

break;

使用类似的东西而不是 try catch block 。这样它就会一直监听,直到您获得有效的num1。对其他号码也进行同样的操作。

关于java - 使用 Java 计算器创建无效输入的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32765121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com