2014-10-28 39 views
0

我對編程頗爲陌生,因爲這是我在大學的第一個學期,沒有任何先驗知識。現在在Python工作後,我們正在開發一個算命程序。我遇到的主要問題是試圖返回交換機,詢問用戶是否想再次玩遊戲,或者如果他們輸入了8個案例之外的無效回覆。還必須有一個嵌套在另一個while循環中的while循環。在循環切換case和嵌套while循環時遇到問題

Scanner user = new Scanner(System.in); 
    System.out.println("Welcome to the Fortune Telling program.\n"); //Welcome message 

    System.out.print("Would you like me to tell your fortune? Type 1 for yes and any other number for no: "); //ask for yes or no to run 
    int Var0 = user.nextInt(); 
    if (Var0 == 1) 
    { 
     System.out.print("Enter a number 1-8 and I will tell your fortune: "); //ask for number between 1-8 to find fortune or invalid 
     int Var1 = user.nextInt();            
      switch (Var1) 
      { 
      case 1:                    //case 1-8 fortunes 
       System.out.println("\nYou will become great if you believe in yourself."); 
       break; 
      case 2: 
       System.out.println("\nSerious trouble with bypass you."); 
       break; 
      case 3: 
       System.out.println("\nYou will travel to many exotic places in your lifetime."); 
       break; 
      case 4: 
       System.out.println("\nYour ability for accomplishment will follow with success."); 
       break; 
      case 5: 
       System.out.println("\nWhen fear hurts you, conquer it and defeat it!"); 
       break; 
      case 6: 
       System.out.println("\nYou will be called in to fulfill a position of higher honor and responsibility."); 
       break; 
      case 7: 
       System.out.println("\nYour golden opportunity is coming shortly."); 
       break; 
      case 8: 
       System.out.println("\nIntegrity is doing the right thing, even when nobody is watching."); 
       break; 
      default: 
       System.out.print("That's not a valid number. Try again.\n");       //invalid number try to rerun for correct response 
      } 
     }                       //display next print only on case not default 
    System.out.print("Would you like another fortune? Type 1 for yes and any other number for no: "); //loop this back into 'switch' 
    int Var2= user.nextInt(); 

    System.out.print("Thank you for trying the fortune telling program.");       //Thank you message 
    user.close(); 
    } 
} 
+0

爲什麼你需要一個嵌套的while循環? – Chiseled 2014-10-28 21:58:51

+0

它是作業的一部分。但我不明白應該放在哪裏,以便用戶可以根據需要再次運行交換機。 @ Moh123 – 2014-10-28 22:03:20

+0

你的任務可能是由不擅長編程的人設定的。恕我直言,你應該使用for循環,而不是while循環,因爲你有一個迭代的方面(重新詢問用戶是否輸入了無效的響應)。另外,這個任務不應該指定一個實現 - 選擇一個是學習的一部分。 – Bohemian 2014-10-28 22:22:46

回答

0

您不需要嵌套循環。雖然循環做得很好。您也只需要一個Var0,這也是保持/停止循環的條件。整個代碼位於try-catch塊內部,用於在用戶輸入非int的內容時解決問題。最後在最後關閉掃描儀 - 不管有沒有。

 Scanner user = new Scanner(System.in); 
     try { 

      System.out.println("Welcome to the Fortune Telling program.\n"); 
      System.out 
        .print("Would you like me to tell your fortune? Type 1 for yes and any other number for no: "); 
      int Var0 = user.nextInt(); 
      while (Var0 == 1) { 
       System.out 
         .print("Enter a number 1-8 and I will tell your fortune: "); 
       int Var1 = user.nextInt(); 
       switch (Var1) { 
       case 1: // case 1-8 fortunes 
        System.out 
          .println("\nYou will become great if you believe in yourself."); 
        break; 
       case 2: 
        System.out.println("\nSerious trouble with bypass you."); 
        break; 
       case 3: 
        System.out 
          .println("\nYou will travel to many exotic places in your lifetime."); 
        break; 
       case 4: 
        System.out 
          .println("\nYour ability for accomplishment will follow with success."); 
        break; 
       case 5: 
        System.out 
          .println("\nWhen fear hurts you, conquer it and defeat it!"); 
        break; 
       case 6: 
        System.out 
          .println("\nYou will be called in to fulfill a position of higher honor and responsibility."); 
        break; 
       case 7: 
        System.out 
          .println("\nYour golden opportunity is coming shortly."); 
        break; 
       case 8: 
        System.out 
          .println("\nIntegrity is doing the right thing, even when nobody is watching."); 
        break; 
       default: 
        System.out.print("That's not a valid number. Try again.\n"); 
       } 
       System.out 
         .print("Would you like another fortune? Type 1 for yes and any other number for no: "); 
       Var0 = user.nextInt(); 
      } 

      System.out 
        .print("Thank you for trying the fortune telling program."); 
     } catch (Exception e) { 
System.out.println("This is what you tell if user types something which is not a digit"); 
     } finally { 
      user.close(); 
     } 
0

的概念有點幫助:

當你說「我需要返回到交換機......」,這意味着你需要一個循環。

什麼是需要重複的部分?你(或者說,你的老師)可能期望的行爲是在告訴財富後,「你願意我告訴你的財富」這個問題會再次出現。這意味着你必須把它和它所帶來的一切(財富告訴自己)放在一個循環中。

在這種情況下,通常的結構是

  • 顯示問題
  • 獲取用戶輸入
  • 環,但條件是用戶沒有進入「完成」輸入
    • 什麼就做什麼用戶輸入需要的任務。
    • 再次顯示問題
    • 再次獲取用戶輸入,以便下一次循環檢查條件時,它將具有計算的新值。

你能想到你的程序是適合這種模式的零件?

現在接下來的事情是你需要一段時間。這是一個提示:程序希望用戶輸入值1-8。如果他進入'9'或'0'或其他什麼的話,那麼程序是否應該忽略這一點,並再次問他是否想要這筆財富告訴他,還是應該堅持?

+0

這實際上幫了我很多!我會盡量玩弄這個並找出答案。我真的不想直接回答,所以我可以自己弄清楚。下次我希望回覆它會被糾正。再次感謝你! – 2014-10-28 22:18:00

0

請嘗試以下操作。您不需要var1

public static void main(String[] args) 
{ 
    // TODO Auto-generated method stub 
    Scanner user = new Scanner(System.in); 
    System.out.println("Welcome to the Fortune Telling program.\n"); //Welcome message 

    System.out.print("Would you like me to tell your fortune? Type 1 for yes and any other number for no: "); 

    int Var0 = 0; 

    while(Var0 != -1) 
    { 
     System.out.print("Enter a number 1-8 and I will tell your fortune or -1 to quit "); //ask for number between 1-8 to find fortune or invalid 

     Var0 = user.nextInt(); 

     switch (Var0) 
     { 
     case 1:                    //case 1-8 fortunes 
      System.out.println("\nYou will become great if you believe in yourself."); 
      break; 
     case 2: 
      System.out.println("\nSerious trouble with bypass you."); 
      break; 
     case 3: 
      System.out.println("\nYou will travel to many exotic places in your lifetime."); 
      break; 
     case 4: 
      System.out.println("\nYour ability for accomplishment will follow with success."); 
      break; 
     case 5: 
      System.out.println("\nWhen fear hurts you, conquer it and defeat it!"); 
      break; 
     case 6: 
      System.out.println("\nYou will be called in to fulfill a position of higher honor and responsibility."); 
      break; 
     case 7: 
      System.out.println("\nYour golden opportunity is coming shortly."); 
      break; 
     case 8: 
      System.out.println("\nIntegrity is doing the right thing, even when nobody is watching."); 
      break; 
     default: 
      System.out.print("That's not a valid number. Try again.\n"); //invalid number try to rerun for correct response 
     } 

    } 
    System.out.print("Thank you for trying the fortune telling program.");//Thank you message 
    user.close(); 
} 

將代碼放在try catch塊中,只要確保您將捕獲使用掃描儀時可能發生的任何異常。在finally塊中關閉掃描器,或者如果您使用java 1.7或更高版本,則使用try-with-resources更好。