我試圖在程序中使用if else
語句作爲菜單選擇。每當我輸入給定的數字以退出應用程序時,應用程序仍然運行。我不知道我應該怎麼做才能解決這個問題,但任何幫助將不勝感激。提前致謝。程序不會終止 - Java
import java.util.Scanner;
public class Gradebook {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("(1) Average grades for a new student");
System.out.println("(2) Quit");
int selection = input.nextInt();
if(selection == 1) {
System.out.println("Grades will be put in accordingly: \n Homework \n Quizzes \n Tests");
System.out.println("Once all grades have been put in, type -1 to continue");
String name = input.next();
}
else{
System.out.println("Goodbye.");
}
int total = 0;
int counter = 0;
System.out.println("Please enter numeric homework grade or \"-1\" to quit: ");
int grade = input.nextInt();
while(grade != -1) {
total = total + grade;
counter = counter + 1;
grade= input.nextInt();
}
if (counter > 0) {
double average = total/(double)counter;
System.out.println("The average for Homework is: " + average);
}
else
System.out.println("No grades were entered.");
System.out.println("Please enter numeric Quiz grade or \"-1\" to quit: ");
while(grade != -1) {
total = total + grade;
counter = counter + 1;
grade = input.nextInt();
}
if(counter > 0) {
double average = total/(double)counter;
System.out.println("The average for Quizzes is: " + average);
}
else
System.out.println("No grades were entered.");
System.out.println("Please enter numeric Test grade or \"-1\" to quit: ");
while(grade != -1) {
total = total + grade;
counter = counter + 1;
grade = input.nextInt();
}
if(counter > 0) {
double average = total/(double)counter;
System.out.println("The average for Tests is: " + average);
}
else
System.out.println("No grades were entered.");
}
}
嘗試'System.exit(0);'退出。 – APerson 2014-11-01 03:21:27