Java 循环菜单以在选择后显示
所以我有这个代码:Java 循环菜单以在选择后显示,java,loops,Java,Loops,所以我有这个代码: 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();
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),有人知道为什么会发生这种情况吗?
任何帮助都将不胜感激 不太确定您要做什么,但我看到您有一个while循环,并且您的第一个选择if语句与第二个选择if语句是分开的。如果(选项==2)或者我误解了您的意图,您是否打算执行其他操作?您需要添加一个特殊条件并跟踪此特殊情况(最好通过
布尔值
)提示:不要复制和粘贴菜单,将所有System.out.print和println语句放入它们自己的方法中,并在需要执行时调用该方法:public static void printMenu(){/*所有打印语句*/}@WillNewton是的,这将使整个过程更容易,但我的教授不希望我们创建一个方法,而是使用循环来实现,他只是喜欢让生活变得更复杂…你的教授想让你用复制粘贴?这就像父母让孩子抽一整包烟。因此,菜单不会再次出现,但需要提示用户进行其他选择。很抱歉。这是一个错误,我错误地把分号放在那里。@user3404391这个分号很好,但我想这个答案的意思是,也许你的意思是你的条件if(choice==2)
真的是else if(choice==2)
噢,是的,我改变了,我很困惑,因为分号在那里,它不允许我做else,如果我删除它,我就可以做else,如果它现在起作用的话。我已经改变了我的编码,如果你们想要的话,我可以把它贴出来进行评论。如果这解决了问题,请随意选择我的答案。