我对此很新。我刚刚开始上大学的编程课程(CS 107和CS 108 intro to java)。我的作业是在计算机和用户之间创建一个Rock,Paper Scissor游戏。我收到了关于我的compPlay字符串尚未声明的java上的错误消息。由于@caps锁定,它已经得到修复。现在,在我运行程序之后它会打印指令,但是一旦我输入输入(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
答案 0 :(得分:1)
您不能将.equals()
与字符串一起使用来与int进行比较。我建议做的是将userPlay的类型更改为int并进行以下更改。主要之后的第一行:
int userPlay;
现在将userPlay = scan.next();
更改为:
userPlay = scan.nextInt();
比较userPlay时,将几个if语句更改为:
if(userPlay == compInt)
if(userPlay == 0)
if(userPlay == 2)
... etc
我认为你根本不需要字符串,因为你输入的是简单的数字,而不是像剪刀这样的字。您可以将此代码转到Code Review,他们可以帮助您改进它!我建议在数据类型之间进行更多比较!希望这有帮助。
答案 1 :(得分:0)
如果你想编译,你需要先初始化..
String compPlay = "";
答案 2 :(得分:0)
你是一名学生,所以你可以原谅在主要方法中写一段代码,但是当你在编程教育中走得更远时,需要考虑以下事项:< / 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);
}
}
}
}