2015-12-02 17 views
1

/當我運行該程序時。在交換機中的if語句中添加信用點後,總信用超過20後,環路/交換機不會中斷環路。它繼續運行,添加信用。需要解決什麼?/在開關不工作的if語句中斷

import java.util.ArrayList; 
import java.util.Scanner; 
public class CoursesRegistration { 

    public static void main(String[] args) { 
     Scanner kb=new Scanner(System.in); 
     int choose=0; 
     int option; 

     ArrayList<String> CourseOffer = new ArrayList<String>(); 

     CourseOffer.add(" CS1001-Basic Programming    3 Credit  RM300.00"); 
     CourseOffer.add(" CS1002-Python Programming    4 Credit  RM400.00"); 
     CourseOffer.add(" CS1004-Operating System    3 Credit  RM300.00"); 
     CourseOffer.add(" CS1005-Web Graphical Interface (GUI) 3 Credit  RM300.00"); 
     CourseOffer.add(" CS1006-Web Programming    4 Credit  RM400.00"); 
     CourseOffer.add(" CS1007-Agile Development    3 Credit  RM300.00"); 
     CourseOffer.add(" CS1008-Database Implementation  3 Credit  RM300.00"); 
     CourseOffer.add(" CS1009-System Security    3 Credit  RM300.00"); 
     CourseOffer.add(" CS1010-Software Testing    3 Credit  RM300.00"); 
     CourseOffer.add(" NA1001-Culture and Social    2 Credit  RM300.00"); 

     do{ 

      double cost=0; 
      for(int i=0;i<CourseOffer.size();i++) 
      { 
       System.out.println((i+1)+"-"+CourseOffer.get(i)); 
      } 
      System.out.println(""); 

      int total=0; 
      while(choose != 99) 
      { 
       if(total >= 20) 
       { 
        break; 
       } 

       System.out.print("Please choose your subject recomend for this semester: "); 
       choose=kb.nextInt(); 

        switch(choose){ 
          case 1: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1001 Basic Programming"); 
           total +=3; 
           cost+=300; 
          } 
          break; 

          case 2: 
          if(total+4>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1002 Python Programming "); 
           total +=4; 
           cost+=400; 
          } 
          break; 

          case 3: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1004 Operating System"); 
           total += 3; 
           cost+=300; 
          } 
          break; 

          case 4: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1005 Web Graphical Interface (GUI "); 
           total += 3; 
           cost+=300; 
          } 
          break; 

          case 5: 
          if(total+4>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1006 Web Programming "); 
           total += 4; 
           cost+=400; 
          } 
          break; 

          case 6: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1007 Agile Development "); 
           total+= 3; 
           cost+=300; 
          } 
          break; 

          case 7: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1008 Database Implementation "); 
           total+= 3; 
           cost+=300; 
          } 
          break; 

          case 8: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1009 System Security "); 
           total+= 3; 
           cost+=300; 
          } 
          break; 

          case 9: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("CS1010 Software Testing "); 
           total+= 3; 
           cost+=300; 
          } 
          break; 

          case 10: 
          if(total+3>20) 
          { 
           break; 
          } 
          else 
          { 
           System.out.println("NA1001 Culture and Social "); 
           total+= 2; 
           cost+=200; 
          } 
          break; 

        } 

       } 

       System.out.println(""); 
       System.out.println("Total credit hour for this semester is "+total); 
       System.out.println(""); 

       System.out.println("Registration slip of ABC's College Registration Course "); 
       System.out.println(""); 

       for(int i=0;i<CourseOffer.size();i++) 
       { 
        System.out.println(""+CourseOffer.get(i)); 
       } 
       System.out.print("Total student payment : RM"); 
       System.out.printf("%.2f",cost); 

       System.out.println("\nPlease enter 1 to continue and 0 to exit: "); 
       option=kb.nextInt(); 

     }while(option==1);   
    } 
} 
+2

你打破了'之開關語句不'loop' ... – brso05

+0

這個程序實際上是爲一個表示一個課程所有屬性的類而討論的,一個List或者一個實例數組r介紹課程選項及其UI索引編號。整個ginormous switch語句可以被大約十行代碼所取代,並且更改課程列表將變得輕而易舉。 –

回答

1

你打破了switch聲明不是loop的。也許創建一個boolean變量,並將其設置爲true是否應該breakloop那麼switch語句後檢查變量,看它是否是true如果是做另一break ...

+0

或者只是使用標籤打破。 –

+1

感謝您的幫助。 JDev解決方案修復了它:D –

0

爲什麼不這樣做:

while(choose != 99 && total <= 20){

添加循環條件檢查中的條件......

1

變化

while(choose != 99) 
{ 

myLoop: 
while(choose != 99) 
{ 

&的開關塊內,你可以使用打破循環

break myLoop; 
+0

我用過這個。它是完美的解決方案。非常感謝:DDD –

+0

@AkmalHafizin不用客氣。請標記答案。 –

+0

如何標記???? –