2011-08-31 53 views
1

我的switch語句只是保持循環。它應該打印您選擇的選項,然後重新打印菜單。請幫忙! 這裏是我的代碼:在Java中切換循環

menu ="\n\t1 Create Account" + 
      "\n\t2 Check balance" + 
      "\n\t3 Withdraw" + 
      "\n\t1 Deposit" + 
      "\n\t2 Get account ID" + 
      "\n\t3 Set ID" + 
      "\n\t1 Display Account Info" + 

      "\n\t0 Quit\n\n\n"; 

    System.out.println(menu); 
    System.out.println("\tEnter your selection: "); 
    option = scan.nextInt(); 

while (option != 0) { 

      switch (option) { 

      case 1: // Enter and Validate SSN 
         System.out.print("option 1"); 
         break; 

      case 2:  //Enter and Validate Passwords 
         System.out.print("option 2"); 
         break; 

      case 3:  //Enter,Verify, and Translate a Phone keypad Number 
         System.out.print("option 3"); 
         break; 

      case 4: // Enter and Validate SSN 
         System.out.print("option 4"); 
         break; 

      case 5:  //Enter and Validate Passwords 
         System.out.print("option 5"); 
         break; 

      case 6:  //Enter,Verify, and Translate a Phone keypad Number 
         System.out.print("option 6"); 
         break; 

      case 7:  //Enter,Verify, and Translate a Phone keypad Number 
         System.out.print("option 7"); 
         break; 

      default: outputString = "\nInvalid Selection\n"; 
         System.out.println(outputString); 
         break; 

     } 
    } 
+0

爲什麼你需要擺在首位的循環? 「if」語句是否足夠?如果你想重印菜單,循環現在不會那樣做;也許循環應該包含該塊。 –

+0

不,它需要在輸入您的選擇後繼續打印菜單。 0退出 – Josh

+1

這就是我說的循環不會那樣做的意思。您的循環體只會打印該選項,直到它爲0,並且主體中沒有任何設置將其設置爲0.您還需要一個'do ... while'循環以及打印邏輯。 –

回答

7

我敢肯定,這是你的while循環,是做循環。而且你永遠不會改變循環體內的option的值,所以它當然會連續運行。

據推測,要招行:

option = scan.nextInt(); 

到循環的第一行:

while (option != 0) { 
    option = scan.nextInt(); 
    ... 
} 
+0

我需要它在你選擇後再次打印菜單。它只是不斷打印你現在按下的選項。 – Josh

+1

@Josh,我沒跟着你。如果你採納我的建議,它會打印出你剛輸入的選項。這不是你想要的嗎?如果你想看每個*時間的菜單,那麼將這些東西移動到循環體中。 –

+0

哇,好的,謝謝。我只是最終把它放在循環的最後回憶。你所說的只會簡單地做同樣的事情。謝謝! – Josh

2

選項在你有while循環從未改變 - 從而導致無限循環如果您輸入的任何內容不是0

0

您的代碼一直處於循環狀態,因爲您在循環中時option的值永遠不會改變。你需要以不同的方式編寫你的邏輯。順便說一句,switch不循環,while做循環。

1

一個do while循環將需要更少的代碼:

menu ="\n\t1 Create Account" + 
     "\n\t2 Check balance" + 
     "\n\t3 Withdraw" + 
     "\n\t1 Deposit" + 
     "\n\t2 Get account ID" + 
     "\n\t3 Set ID" + 
     "\n\t1 Display Account Info" + 

     "\n\t0 Quit\n\n\n"; 

do { 
    System.out.println(menu); 
    System.out.println("\tEnter your selection: "); 
    option = scan.nextInt(); 
    switch (option) { 

    case 1: // Enter and Validate SSN 
    System.out.print("option 1"); 
    break; 

    case 2:  //Enter and Validate Passwords 
    System.out.print("option 2"); 
    break; 

    case 3:  //Enter,Verify, and Translate a Phone keypad Number 
    System.out.print("option 3"); 
    break; 

    case 4: // Enter and Validate SSN 
    System.out.print("option 4"); 
    break; 

    case 5:  //Enter and Validate Passwords 
    System.out.print("option 5"); 
    break; 

    case 6:  //Enter,Verify, and Translate a Phone keypad Number 
    System.out.print("option 6"); 
    break; 

    case 7:  //Enter,Verify, and Translate a Phone keypad Number 
    System.out.print("option 7"); 
    break; 

    default: outputString = "\nInvalid Selection\n"; 
    System.out.println(outputString); 
    break; 

    } while (option != 0) 


}