Java 协助玩石头、纸、剪刀游戏

Java 协助玩石头、纸、剪刀游戏,java,Java,我对这个很陌生。我刚开始在大学上编程课(CS107和CS108 java简介)。我的家庭作业是在电脑和用户之间制作一个石头、剪纸的游戏。我在java上收到一条错误消息,我的compPlay字符串上没有声明。由于@caps lock,它已经被修复。现在,在我运行程序后,它会打印指令,但一旦我输入输入(0、1或2),它不会告诉我是平局,是赢了还是输了。有人能认出我把事情搞砸了吗?谢谢 import java.util.Scanner; import java.util.Random; publi

我对这个很陌生。我刚开始在大学上编程课(CS107和CS108 java简介)。我的家庭作业是在电脑和用户之间制作一个石头、剪纸的游戏。我在java上收到一条错误消息,我的compPlay字符串上没有声明。由于@caps lock,它已经被修复。现在,在我运行程序后,它会打印指令,但一旦我输入输入(0、1或2),它不会告诉我是平局,是赢了还是输了。有人能认出我把事情搞砸了吗?谢谢

import java.util.Scanner;

import java.util.Random;

public class Lab3 {

    public static void main(String[] args) 
{
        String userPlay; // 0=Scissor, 1=Rock, 2=Paper
        String compPlay = ""; // 0=Scissor, 1=Rock, 2=Paper
        int compInt; // Random generated number from 0-2

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
        System.out.print("Let's play Rock, Paper, Scissor! Please enter 0 for Scissor, 1 for Rock or 2 for Paper.  ");
        System.out.println();

        // Random generated number from 0-2
        compInt = (int)(Math.random())*3;


    // Translate computer random number selected into string

    if (compInt == (0))
{
    compPlay = "Scissor";
}
    else if (compInt == (1))
{
    compPlay = "Rock";
}
    else if (compInt == (2))
{
    compPlay = "Paper";
}

    // Get user input
    System.out.println("Enter your choice ");
    userPlay = scan.next();

    // Print computer random number
    System.out.println("Computer is: " + compInt);

    // Set who wins, loses or ties

    if(userPlay.equals (compInt))
{
    System.out.println("It's a tie!");
}
    else if (userPlay.equals (1))
{
    if (compPlay.equals("0"))
    System.out.println("Computer is Scissor. You are Rock. You win!");
    else if (compPlay.equals("2"))
    System.out.println("Computer is Paper. You are Rock. You lose.");
}

    else if (userPlay.equals(2))
{
    if (compPlay.equals("Scissor"))
    System.out.println("Computer is Scissor. You are Paper. You lose.");
    else if (compPlay.equals("Rock"))
    System.out.println("Computer is Rock. You are Paper. You win!");
}
    else if (userPlay.equals(0))
{
    if (compPlay.equals("Rock"))
    System.out.println("Computer is Rock. You are Scissor. You lose.");
    else if (compPlay.equals("Paper"))
    System.out.println("computer is Paper. You are Scissor. You win!");
}

}// main
}// class

如果要编译,需要先初始化

String compPlay = "";

您不能对字符串使用
.equals()
与int进行比较。我建议您将userPlay的类型更改为int,并进行以下更改。主线后的第一行:

int userPlay;
现在更改
userPlay=scan.next()至:

userPlay = scan.nextInt();
在比较userPlay时,将几个if语句更改为:

if(userPlay == compInt)
if(userPlay == 0)
if(userPlay == 2)
... etc

我认为这里根本不需要字符串,因为您输入的是简单的数字,而不是剪刀之类的单词。您可以将此代码移交给,他们可以帮助您改进它!我还建议在数据类型之间进行更多的比较!希望这有帮助。

< P>你是一个学生,所以在一个主要的方法中,你可以原谅写一堵代码墙,但是当你在你的程序教育中继续前进的时候,这里有一些要考虑的事情:

Java是一种面向对象的语言。从封装在对象中的行为开始思考,如下所示:

选择:

package game.rps;

/**
 * Choices for rock paper scissors
 * @author Michael
 * @link  https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:42 PM
 */
public enum Choice {
    ROCK, PAPER, SCISSORS
}
结果:

package game.rps;

/**
 * Outcome for rock paper scissors
 * @author Michael
 * @link https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:44 PM
 */
public enum Outcome {
    LOSE, DRAW, WIN
}
播放器代表计算机:

package game.rps;

import java.util.Random;

/**
 * Player represents the computer in the game
 * @author Michael
 * @link https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:54 PM
 */
public class Player {

    private Random random;

    public Player() {
        this.random = new Random();
    }

    public Player(long seed) {
        this.random = new Random(seed);
    }

    public Choice decide() {
        int index = this.random.nextInt(Choice.values().length);
        return Choice.values()[index];
    }
}
玩游戏的驱动程序:

package game.rps;

import java.util.Scanner;

/**
 * Game loop for playing rock paper scissors
 * @author Michael
 * @link https://stackoverflow.com/questions/26310090/assistance-with-rock-paper-scissor-game
 * @since 10/10/2014 9:43 PM
 */
public class Game {

    public static void main(String[] args) {
        String input;
        Scanner in = new Scanner(System.in);
        Game game = new Game();
        Player player = new Player();
        do {
            System.out.println("ROCK, PAPER, SCISSORS, or QUIT: ");
            input = in.nextLine().trim();
            Choice human = Choice.valueOf(input);
            Choice computer = player.decide();
            System.out.println(String.format("computer: %s", computer));
            System.out.println(String.format("human   : %s", human));
            System.out.println(String.format("outcome : %s", game.play(human, computer)));
        } while (!"QUIT".equalsIgnoreCase(input));
    }

    public  Outcome play(Choice player1, Choice player2) {
        if (player1 == player2) {
            return Outcome.DRAW;
        } else {
            if (player1 == Choice.ROCK) {
                return (player2 == Choice.PAPER ? Outcome.LOSE : Outcome.WIN);
            } else if (player1 == Choice.PAPER) {
                return (player2 == Choice.SCISSORS ? Outcome.LOSE : Outcome.WIN);
            } else {
                return (player2 == Choice.ROCK ? Outcome.LOSE : Outcome.WIN);
            }
        }
    }
}

字符串
永远不等于
整数
(一个
整数
)。是的!非常感谢你!它清除了错误消息。我能运行它。。但当我输入时,并不是说我输了、赢了还是平手。我错过了什么?