我剛開始在一堂課中學習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的麻煩,它會說「無效的條目中的結果將是無效的。 ..「但它仍然會用程序的其餘部分執行一個無效的數字。
如何讓程序停止並提示用戶輸入有效的數字,然後執行程序?此外,這是我所做的第三個程序,所以任何其他建議,將不勝感激。
感謝
我不認爲你閱讀的整個問題。 (提示:新程序員並不總是使用正確的術語。) – ajb
我正在處理它... :) –
對於任何混淆抱歉,我真的不明白這些條款。 我們所討論的唯一循環是,雖然老師不在意我不知道該怎麼做,但我只想知道我的個人知識 – user3344771