所以我有这段代码:
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(选项== 1)仍然在我按下不同的数字后执行。执行完后,菜单再次弹出,如果我点击相同的数字,它将执行。 例如,当我的第一个选择是3时,但它只会执行if(choice == 1),我的第二个选择将是2并且将执行if(choice == 2),而我的第三个选择将再次为3 ,但它会回到if(选择== 1),有人会知道为什么会这样吗? 任何帮助将不胜感激。
答案 0 :(得分:0)
不太确定你要做什么,但我看到你有一个while循环,如果声明与你的第二选择if语句分开,你的第一选择。如果(选择== 2)或者我误解了你想要做什么,你的意思是做其他事吗?