【发布时间】:2014-08-02 00:24:34
【问题描述】:
我知道这个 java 任务已经一次又一次地被问到这里,但是我不想复制其他方法(因为似乎有很多方法可以编写这种代码),我宁愿在地雷上得到帮助我可以更容易地理解这些概念。还有,教授应该很容易就能查到我有没有作弊,哈哈。
这段代码没有错误,但它不能像预期的那样工作。输出真的是……随机的。
我觉得我错过了整个代码部分,但我一遍又一遍地阅读我的教科书,似乎无法发现错误的根源。 :|请帮忙!
编辑:我用固定的更改更新了我的帖子。
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
String playerTwo = "";\\to be honest I'm not sure what this is even for
Scanner keyboard = new Scanner(System.in);
System.out.println("Lets play rock-paper-scissors! Please enter your move:");
int Choice = (int) (Math.random()*2);
if (Choice == 0)
playerTwo = "ROCK";\\rock = 0
else if (Choice == 1)
playerTwo = "PAPER";\\paper = 1
else if (Choice == 2)
playerTwo = "SCISSORS";\\scissors = 2
String playerOne = keyboard.nextLine();
playerOne = playerOne.toUpperCase();
if (playerOne.equals(playerTwo)){
System.out.println("It's a tie, so nobody wins!");
}
if (playerOne.equals("ROCK")){
if (playerTwo.equals("PAPER")){
System.out.println("Your rock got covered by my paper. You lose!");
}
if (playerTwo.equals("SCISSORS")){
System.out.println("Your rock smashes my scissors. You win!");
}
}
if (playerOne.equals("PAPER")){
if (playerTwo.equals("ROCK")){
System.out.println("Your paper covers my rock. You win!");
}
if (playerTwo.equals("SCISSORS")){
System.out.println("Your paper got cut by my scissors. You lose!");
}
}
else if (playerOne.equals("SCISSORS")){
if (playerTwo.equals("ROCK")){
System.out.println("Your scissors got smashed by my rock. You lose!");
}
if (playerTwo.equals("PAPER")){
System.out.println("Your scissors cut my paper. You win!");
}
}
else
System.out.println("Error. Please restart and enter either: \"rock\", \"paper\", or \"scissors\".");
}
}
【问题讨论】: