2014-11-01 206 views
-1

我試圖在程序中使用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."); 

    } 

} 
+0

嘗試'System.exit(0);'退出。 – APerson 2014-11-01 03:21:27

回答

4

我假設這是應該退出代碼:

else{ 
     System.out.println("Goodbye."); 
    } 

打印「再見」,但是,不退出應用程序。試試這個:

else{ 
     System.out.println("Goodbye."); 
     System.exit(0); // causes the application to end right here 
    } 

或:

else{ 
     System.out.println("Goodbye."); 
     return; // returns from the main function 
    } 
+0

現在工作就像一個魅力。謝謝。 – 2014-11-01 03:26:21

0

有兩種方法可以做到這一點:改變if語句包含return,或者改變if語句包含System.exit(0)

隨着return

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."); 
     return; 
    } 

隨着System.exit

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."); 
     System.exit(0); 
    }