gpt4 book ai didi

java - 我怎样才能使这段代码更简单?另外我如何检查输入是否是 int?

转载 作者:行者123 更新时间:2023-12-02 00:24:34 24 4
gpt4 key购买 nike

嗨,我是 Java 初学者。我怎样才能让这个拜斯计算器更简单?另外,如果 num1 或 num2 不是整数,我希望用户收到错误。我怎样才能做到这一点?

我已经尝试过 parseint 和 Scanner.hasnextint 但无法让它们工作。

import java.util.Scanner;

public class topfirst{
public static void main(String[] args) {
int num1;
int num2;
int result;
String yesorno;
String yes = "yes";
String op;
while(yesorno.equals(yes)){

System.out.println("Please enter the first number :");
Scanner inputnum1 = new Scanner(System.in);
num1 = inputnum1.nextInt();

System.out.println("Please enter the second number :");
Scanner inputnum2 = new Scanner(System.in);
num2 = inputnum2.nextInt();

System.out.println("Please enter an operation :");
Scanner inputop = new Scanner(System.in);
op = inputop.next();
String minus = "-";
String plus = "+";
String multipl = "*";
String div = "/";
while ((!op.equals(minus)) && (!op.equals(plus)) && (!op.equals(multipl)) && (!op.equals(div)))
{
System.out.println("Incorrent operation, try again.");
op = inputop.next();
}
switch(op){
case "+":
result = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + result);
break;
case "-":
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
break;
case "*":
result = num1 * num2;
System.out.println(num1 + " * " + num2 + " = " + result);
break;
case "/":
result = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + result);
break;


}
System.out.println("Do you want to calculate something else?");
Scanner inyesorno = new Scanner(System.in);
yesorno = inyesorno.next();
}}
}

最佳答案

import java.util.Scanner;

public class TopFirst {

private static final String PLUS = "+";
private static final String MINUS = "-";
private static final String MULTIPLY = "*";
private static final String DIV = "/";

private static final Scanner INPUT = new Scanner(System.in);

public static void main(String[] args) {

do {

System.out.print("Please enter the first number: ");

int num1 = readInt();

System.out.print("Please enter the second number: ");

int num2 = readInt();

System.out.print("Please enter an operator: ");

String operator = readOperator();

int result = countResult(num1, num2, operator);
System.out.printf("%d %s %d = %d%n", num1, operator, num2, result);

System.out.println("Do you want to calculate something else?");

} while (INPUT.next().equals("yes"));
}

private static String readOperator() {
String operator = INPUT.next();

if (isCorrect(operator)) {
return operator;
} else {
System.out.print("Incorrect operator, try again: ");
return readOperator();
}
}

private static int readInt() {
try {
return Integer.parseInt(INPUT.next());
} catch (NumberFormatException e) {
System.out.print("Incorrect number, try again: ");
return readInt();
}
}

private static boolean isCorrect(String operator) {
return MINUS.equals(operator)
|| PLUS.equals(operator)
|| MULTIPLY.equals(operator)
|| DIV.equals(operator);
}

private static int countResult(int num1,
int num2,
String operator) {
switch (operator) {
case PLUS:
return num1 + num2;
case MINUS:
return num1 - num2;
case MULTIPLY:
return num1 * num2;
case DIV:
return num1 / num2;
default:
return 0;
}
}
}

关于java - 我怎样才能使这段代码更简单?另外我如何检查输入是否是 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059874/

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