java循环菜单以在选择后显示
所以我有以下代码:
import java.util.Scanner;
public class StudentGradeDriver
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter student name: ");
String name = scanner.nextLine();
StudentGrade grade = new StudentGrade(name);
System.out.println("Welcome to the Student Grade Interface!");
System.out.println("You are modyfing the grades for John Doe");
System.out.println("===============================================");
System.out.println("(1) Add homework score");
System.out.println("(2) Add passed Lab");
System.out.println("(3) Add midterm exam score");
System.out.println("(4) Set final exam score");
System.out.println("(5) Calculate final grade");
System.out.println("(-1) Exit");
System.out.println("===============================================");
System.out.print("Please input your choice: ");
int choice = scanner.nextInt();
while(true)
{
if (choice == 1);
{
System.out.println("Enter total points earned: ");
double _hwEarned = scanner.nextDouble();
System.out.println("Enter total points possible: ");
double _hwPossible = scanner.nextDouble();
grade.addHomework(_hwEarned, _hwPossible);
System.out.println("Welcome to the Student Grade Interface!");
System.out.println("You are modyfing the grades for John Doe");
System.out.println("===============================================");
System.out.println("(1) Add homework score");
System.out.println("(2) Add passed Lab");
System.out.println("(3) Add midterm exam score");
System.out.println("(4) Set final exam score");
System.out.println("(5) Calculate final grade");
System.out.println("(-1) Exit");
System.out.println("===============================================");
System.out.print("Please input your choice: ");
choice = scanner.nextInt();
}
if (choice == 2)
{
System.out.println("A passed lab has been added for "
+ "this student. ");
grade.passedLab();
}
else if (choice == 3)
{
System.out.println("Enter total points earned for midterm: ");
double _midEarned = scanner.nextDouble();
System.out.println("Enter total points possible for midterm: ");
double _midPossible = scanner.nextDouble();
grade.addExam(_midEarned, _midPossible);
System.out.println("Welcome to the Student Grade Interface!");
System.out.println("You are modyfing the grades for John Doe");
System.out.println("===============================================");
System.out.println("(1) Add homework score");
System.out.println("(2) Add passed Lab");
System.out.println("(3) Add midterm exam score");
System.out.println("(4) Set final exam score");
System.out.println("(5) Calculate final grade");
System.out.println("(-1) Exit");
System.out.println("===============================================");
System.out.print("Please input your choice: ");
choice = scanner.nextInt();
}
else if (choice == 4)
{
System.out.println("Enter total points earned for final: ");
double _finalEarned = scanner.nextDouble();
grade.setFinalExam(_finalEarned);
System.out.println("Welcome to the Student Grade Interface!");
System.out.println("You are modyfing the grades for John Doe");
System.out.println("===============================================");
System.out.println("(1) Add homework score");
System.out.println("(2) Add passed Lab");
System.out.println("(3) Add midterm exam score");
System.out.println("(4) Set final exam score");
System.out.println("(5) Calculate final grade");
System.out.println("(-1) Exit");
System.out.println("===============================================");
System.out.print("Please input your choice: ");
choice = scanner.nextInt();
}
else if (choice == 5)
{
System.out.println("The score for " + name + " is "
+ grade.getFinalScore());
}
else if(choice == -1)
{
break;
}
}
}
}
是否有一种更简单或其他的方法来循环它,以便菜单将显示在选项1、3、4和5之后,而不是选项2,并且在用户输入为-1时仍然退出
而且,即使我复制和粘贴了菜单,似乎在我按下不同的数字后,if(choice==1)仍然会执行。执行之后,菜单再次弹出,如果我点击相同的数字,它就会执行。
例如,当我的第一个选择是3,但它只会在(选择==1)时执行,我的第二个选择将是2,然后将在(选择==2)时执行,我的第三个选择将再次是3,但它将返回到如果(选择==1),有人知道为什么会发生这种情况吗?
任何帮助都将不胜感激
# 1 楼答案
不太确定你想做什么,但我看到你有一个while循环,你的第一个选择if语句和第二个选择if语句是分开的。如果(选择==2),你是想做其他事情,还是我误解了你的意图