我不确定我是否也正确使用了 public static int 。总而言之,我觉得这段代码是一团糟至。
import java.util.Scanner;
public class Program7 {
public static void main(String[] args) {
// main menu
mm();
int choice = 0;
switch (choice) {
// addition
case 1:
add();
break;
// subtraction
case 2:
sub();
break;
// exit
case 3:
System.out.print("GOOD BYE ^__^");
break;
// if choice >3 or <1
default:
System.out.print("\nInvalid selection.\nPlease select from (1-3): ");
Scanner kb = new Scanner(System.in);
choice = kb.nextInt();
}
}
添加
public static int add() {
System.out.print("\nEnter number1: ");
Scanner kb = new Scanner(System.in);
int num1 = kb.nextInt();
System.out.print("Enter number2: ");
int num2 = kb.nextInt();
int sum = num1 + num2;
System.out.print("\n" + num1 + " + " + num2 + " = " + sum);
return sum;
}
减法
public static int sub() {
System.out.print("Enter number1: ");
Scanner kb = new Scanner(System.in);
int num1 = kb.nextInt();
System.out.print("Enter number2: ");
int num2 = kb.nextInt();
int diff = num1 - num2;
System.out.print(num1 + " - " + num2 + " = " + diff);
return diff;
}
无效数字(<1 或 >3)的主菜单和/或在用户完成选择后直到他们选择选择数字 3:
public static int mm() {
System.out.print("==MAIN MENU==\n1.Addition\n2.Subtraction\n3.Exit\n\nSelect Menu(1-3): ");
Scanner kb = new Scanner(System.in);
int choice = kb.nextInt();
return choice;
}