1

我一直在尝试修复这个程序一段时间。基本上,它是一种石头剪刀布类型的游戏,除了输入验证之外,一切都有效。任何帮助,将不胜感激。

这是我的代码:

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!  
4

2 回答 2

3

当你想比较字符串时,而不是使用==!=使用equals()and方法。equalsIgnoreCase()

并且在使用 a 时Scanner使用该nextLine()方法而不是next().

使用常量而不是变量字符串。

并且当玩家的输入不正确并且您希望玩家再次输入时,最好使用循环结构while,当用户给出有效输入时,然后跳出循环或在函数内使用循环和返回一个值。

喜欢

编辑:回答你的评论问题试试这个

class Player {
  String choice;
}

class Game {

static final ROCK = "ROCK";
static final PAPER = "PAPER";
static final SCISSORS = "SCISSORS";

  Scanner scanner = new Scanner();
  Player[] players = new Player[2]; //assuming you only want 2 players Use ArrayList for unspecified number of users and use a separate method to initialize it.

 public Game {
      for(int i =0 ; i<2 ; i++) {
         players[i]= new Player();
      }
 }

 private String getPlayerChoice() {
    System.out.println("Please enter your choice");
    System.out.println("1.Rock\n2.Paper\n3.Scissors")
    return scanner.nextLine();
 }

 void getInputAndValidate(Player p) {
  p.choice = getPlayerChoice();
  while(true) {
    if(p.choice.equalsIgnorecase(ROCK) || 
       p.choice.equalsIgnorecase(PAPER) || 
       p.choice.equalsIgnorecase(SCISSORS) ) {
         break;
    }
    else {
        System.out.println("Please enter a valid input");
        p.choice = getPlayerChoice(); \\ use the scanner as an instance variable.
    }
  }  
 }

 void getInput() {
    for(int i = 0; i<2;i++) {
     System.out.println("Player " + (i+1));
     getInputAndValidate(players[i]);
    }
 }

void compute(){
  // implement your game logic here
}
public static void main(String[] args) {
  Game g = new Game();
  g.getInput();
  g.compute();
}

}
于 2013-11-04T15:14:59.223 回答
1
if ((player1 != ("rock"))
    || (player1 != ("paper"))
    || (player1 != ("scissors")))

将所有 != 更改为 equals 或 equalsIgnoreCase() 以避免大小写混淆

if ((!player1.equalsIgnoreCase("rock"))
    || (!player1.equalIgnoreCase("paper"))
    || (!player1.equalsIgnoreCase("scissors")))
于 2013-11-04T15:14:50.847 回答