我遇到了一些不应该那么难的事情。 我无法让我的程序正确地播放。我很感激帮助,这是代码。 这也是我的第一篇文章,它可能看起来很奇怪。
package rpsgame;
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
//variables
final int ROCK = 0;
final int PAPER = 1;
final int Scissors = 2;
int played = 0;
boolean playing = true;
Scanner scan = new Scanner(System.in);
Random rng = new Random();
//User and Computer input
int compChose;
int youChose;
//Program
System.out.println("=:=:=| Hello I'm Compy! Let's Play Rock-Paper-Scissors!|=:=:=");
//System.out.println("Here are the rules.\n" + "Rock beats Scissors.\n" + "Scissors beat Paper.\n" + "Paper beats Rock.");
while (playing == true) {
while (played < 6) {
//Ask for some input
System.out.println("Rock = 0\n" + "Paper = 1\n" + "Scissors = 2\n" + "Please enter your choice.\n");
//Get numbers 0 , 1, or 2.
youChose = scan.nextInt();
//case for invalid choice.
if (youChose < 0 || youChose > 3) {
System.out.println("Invalid choice, Game Over.");
System.exit(0);
}
//Generate computers choice
compChose = rng.nextInt(3);
//Draws
if (youChose == compChose) {
if (youChose == 0) {
System.out.println("Both players chose rock!\n" + "It's a tie!");
} else if (youChose == 1) {
System.out.println("Both playes chose paper!\n" + "It's a tie!");
} else {
System.out.println("Both players chose Scissors!\n" + "It's a tie!");
}
played++;
}
//Rock
if (youChose == 0) {
if (compChose == 1) {
System.out.println("You chose rock; Compy chose paper.");
System.out.println("Compy Wins!\n");
} else {
System.out.println("You chose rock; Compy chose scissors.");
System.out.println("You win!\n");
}
played++;
}
//Paper
else if (youChose == 1) {
if (compChose == 0) {
System.out.println("You chose paper; Compy chose rock.");
System.out.println("Compy Wins\n");
} else {
System.out.println("You chose paper; Compy chose scissors.");
System.out.println("Compy wins!\n");
}
played++;
}
//Scissors
else // User chooses scissors
{
if (compChose == 0) {
System.out.println("You chose scissors; Compy chose rock.");
System.out.println("Compy wins!\n");
} else {
System.out.println("You chose scissors; Compy chose paper.");
System.out.println("You win!\n");
}
played++;
}
if (played > 6) {
System.out.println("Do you want to play again? (Yes/No)");
String playagain = scan.nextLine();
if (playagain == "yes") {
playing = true;
} else {
playing = false;
}
}
}
}
}
}
}