我正在用java編寫一個岩石紙剪刀遊戲拼貼,並想知道如何從for循環中減去1。如何從java中的for循環中減去
完整的代碼工作,但如果有人輸入一個無效的數字(低於1或更高,然後3)我的代碼要求他們重新輸入一個數字(1,2,3) 但for循環計算它爲一個循環所以我最終以較少的舉動。
我需要改變一些東西在最後的「否則,如果」但我不能弄明白
可能有人點我在正確的方向?
謝謝。
完整的代碼是這樣的:
import java.io.*;
import java.util.Random;
public class RockPaperScissors {
static int loss = 0;
static int win = 0;
static int tie = 0;
int draw;
static int playerHand;
static int compHand;
int gameLoop;
public void playerMoves() {
if (playerHand == compHand){ //if both hands (player and computer) are the same
System.out.println("Draw, your picked " + playerHand + " and the computer picked " + compHand);
tie++; // add 1 to tie score
}
else if (playerHand == 1 && compHand == 2){ // if player picks Rock and computer picks paper
System.out.println("the computer picks " + compHand + "! " + "Paper beats rock, You lose");
loss++; // add 1 to loss score
}
else if (playerHand == 1 && compHand == 3){ // if player picks rock and computer scissors
System.out.println("the computer picks " + compHand + "! " + "Rock beats Scissors, You win!");
win++; // add 1 to win score
}
else if (playerHand == 2 && compHand == 1){ //if player picks paper and computer picks rock
System.out.println("the computer picks " + compHand + "! " + "Paper beats rock, you win!");
win++; // add 1 to win score
}
else if (playerHand == 2 && compHand == 3){ // if player picks paper and computer scissors
System.out.println("the computer picks " + compHand + "! " + "Scissors beats Paper, you lose!");
loss++; // add 1 to loss score
}
else if (playerHand == 3 && compHand == 1){ // if player picks scissors and computer rock
System.out.println("the computer picks " + compHand + "! " + "Rock beats Scissors, you lose!");
loss++; // add 1 to loss score
}
else if (playerHand == 3 && compHand == 2){ // if player picks scissors and computer paper
System.out.println("the computer picks " + compHand + "! " + "Scissors beats Paper, you win!");
win++; // add 1 to win score
}
else if (playerHand < 1 || playerHand > 3) {
System.out.println(playerHand + " is not a valid number. Try again...");// if not valid number ask again.
gameLoop = gameLoop - 1; // subtract 1 from gameLoop
}
else {
System.out.println("Great job, you broke it...");
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Welcome to Rock Paper Scissors");
System.out.println("Lets play ten games and see if you can outsmart the computer!");
for (int gameLoop = 0; gameLoop < 10; gameLoop++) { // a for loop to keep the game running 10 times
Random randomNumber = new Random(); // create a new random number everytime
compHand = (int) randomNumber.nextInt(3); // generate a random number for the computer (compHand)
compHand++;
// while (playerHand < 1 || playerHand > 3) {
// System.out.println(playerHand + " is not a valid move. Try again...");
System.out.println("Rock(1), Paper(2), or Scissors(3) Please enter the number");
RockPaperScissors draw = new RockPaperScissors();
RockPaperScissors.playerHand = Integer.parseInt(br.readLine());
draw.playerMoves(); // go to public void playerMoves and use that.
System.out.println("the score is: " + win + " Games won. " + loss + " Games lost. " + tie + " Games tie."); // print out the game score at the end of every game
System.out.println("");
}
}
}
這工作就像一個魅力,謝謝。現在要清理一些東西,看看我能否縮小它。 – jaylow