2014-02-24 112 views
0

我剛開始在一堂課中學習Java。這個任務是製作一個Rock,Paper and Scissors遊戲,這裏是我的源代碼。我已經把它翻過來了,但還沒有收到分數。如何在Java中使用if有條件地循環語句?

package rockpaperscissors; 

import java.util.Random; 
import java.util.Scanner; 

public class Main { 

public static void main(String[] args) { 
    // Create scanner input 
    Scanner input = new Scanner (System.in); 

    // Introduce user to game 
    System.out.println("Welcome to the Rock, Paper and Scissors game"); 

    // Ask the user for their move 
    System.out.println("\nPlease select a move\nEnter 0 for Scissors, 1 " 
      + "for Rock and 2 for Paper"); 
    int userMove = input.nextInt(); 

    // Ensure that userMove is between 0 and 2 
    if (userMove > 2 || userMove < 0) { 
     System.out.println("Invalid entry the result of this game will not " 
       + "be accurate! Please retry using either 0 for Scissors, 1" 
       + " for Rock and 2 for Paper\n"); 
    } 

    // Generate computerMove using java.util.Random 
     // Create instance first 
    Random MyRandom = new Random(); 

    //Now generate number 
    int computerMove = MyRandom.nextInt(3); 

    System.out.print("The computer played " + computerMove + "\nRemember" 
      + " 0 stands for Scissors, 1 is for Rock and 2 is for paper\n"); 

    // Determine draw result 
    if (userMove == computerMove) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The game is a draw!"); 
    } 
    // Determine computer win 
    else if(userMove == 0 && computerMove == 1) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The Computer Wins!"); 


    } 

    else if(userMove == 2 && computerMove == 0) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The Computer Wins!"); 

    } 

    else if(userMove == 1 && computerMove == 2) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The Computer Wins!"); 

    } 

    /* Determine User win, as no other moves can result in computer win all 
    other combinations will result in user win 
    */ 

      else { 
     System.out.print("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " You Win!"); 
    } 



} 

}

我有上獲取節目的數量來重新問,如果數量大於2或小於0的麻煩,它會說「無效的條目中的結果將是無效的。 ..「但它仍然會用程序的其餘部分執行一個無效的數字。

如何讓程序停止並提示用戶輸入有效的數字,然後執行程序?此外,這是我所做的第三個程序,所以任何其他建議,將不勝感激。

感謝

回答

1

你想要的是,如果你的條件滿足時無法完成所有的其他代碼。

現在,當滿足條件時,您會發出錯誤消息 - 但不會停止執行其餘功能。

你可以停下來。

所以 - 沒有告訴你確切的答案......你如何退出函數? 這樣做只是在錯誤信息顯示後,你會沒事的。

但是,如果您想要繼續獲取用戶輸入,直到您符合條件...需要在提取和檢查的部分周圍進行循環。

你知道什麼循環?

你知道一個讓你循環,直到你遇到一些條件?

你已經得到了你的條件......現在你只需要調整一個循環讓你做你想要的代碼 - 反覆 - 直到滿足條件。

僞代碼

(即這是不是真正的Java - 你必須翻譯):

some_loop do 
    fetch user input 
    give error message if its not ok 
until (user input is ok) 
+0

我不認爲你閱讀的整個問題。 (提示:新程序員並不總是使用正確的術語。) – ajb

+0

我正在處理它... :) –

+0

對於任何混淆抱歉,我真的不明白這些條款。 我們所討論的唯一循環是,雖然老師不在意我不知道該怎麼做,但我只想知道我的個人知識 – user3344771