我一直在尝试修复这个程序一段时间。基本上,它是一种石头剪刀布类型的游戏,除了输入验证之外,一切都有效。任何帮助,将不胜感激。
这是我的代码:
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
System.out.println("Player 1: Choose rock, paper or scissors:");
String player1 = scan.next() .toLowerCase();
//Player 1 Input Validation
if ((player1 != ("rock"))
|| (player1 != ("paper"))
|| (player1 != ("scissors")))
{
System.out.println("Thats not right, choose rock, paper or scissors");
}
//Send Back to input
System.out.println("Player 2: Choose rock, paper or scissors:");
String player2 = scan.next() .toLowerCase();
//Player 2 Input Validation
if ((player2 != ("rock"))
|| (player2 != ("paper"))
|| (player2 != ("scissors")))
{
System.out.println("Please choose rock, paper or scissors");
}
System.out.println("Player 1 chose " + player1);
System.out.println("Player 2 chose " + player2);
//For Player 1 to win
if((player1.equals("rock"))&&(player2.equals("scissors"))
||(player1.equals("scissors"))&&(player2.equals("paper"))
||(player1.equals("paper"))&&(player2.equals("rock")))
{
System.out.println ("Player 1 Wins!");
}
//For a draw
if (player1.equals(player2))
{
System.out.println ("Its a Draw!");
}
//For Player 2 to win
if ((player2.equals("rock"))&&(player1.equals("scissors"))
||(player2.equals("scissors"))&&(player1.equals("paper"))
||(player2.equals("paper"))&&(player1.equals("rock")))
{
System.out.println("Player 2 wins!");
}
}
这是我得到的输出(Rock and Paper,是用户输入):
Player 1: Choose rock, paper or scissors:
Rock
Thats not right, choose rock, paper or scissors
Player 2: Choose rock, paper or scissors:
Paper
Please choose rock, paper or scissors
Player 1 chose rock
Player 2 chose paper
Player 2 wins!