Java石头剪刀

时间:2014-08-02 00:24:34

标签: java random

我知道这个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\".");
   }
}

3 个答案:

答案 0 :(得分:1)

在我弄清楚你的代码有什么问题之前,我们先提出一些建议:

Math.round()没有必要。如果您要转换为int,那么它将自动舍入到最接近的整数。

您应该使用三种类型的结果声明一个枚举。然后你可以用名字而不是数字或字符串来引用它们:

public enum Outcome { ROCK, PAPER, SCISSOR };

现在您可以分配变量并使用switch语句:

Outcome player1 = Outcome.ROCK;
// switch statement example
switch (player1) {
    case ROCK:
        // do something
    case PAPER:
        // do something
    case SCISSOR:
        // do something

此外,注释使用两个FORWARD斜杠,而不是BACK斜杠

现在代码中的WTF时刻。这到底是什么:

if (playerOne.equals("ROCK")){}
              else if (playerTwo.equals("PAPER")){
                 System.out.println("Your rock got covered by my paper. You lose!");
                 }
              else if (playerTwo.equals("SCISSORS")){
                 System.out.println("Your scissors got smashed by my rock. You win!");
                 }

基本上,你刚刚告诉电脑,如果playerOne选择摇滚,那就什么都不做。 if语句的语法如下:

if (condition) {
     // do something 
}

如果要在if语句中包含多个if语句,请执行以下操作:

if (condition) {
    if (condition) { // do something }
    else if (condition) { // do something else if first did not work }
    else { // do something if nothing worked }
}

在您的代码中输入了以下内容:

if (condition) {} // this does nothing if condition is true
else if (condition) {...} // if the first if statement is true, this will not be reached
else if (condition) {...} // neither will this!

因此,为了解决您的问题,您的代码段必须如下所示:

if (playerOne.equals("ROCK")) {
              if (playerTwo.equals("PAPER")) {
                 System.out.println("Your rock got covered by my paper. You lose!");
              }
              else if (playerTwo.equals("SCISSORS")) {
                 System.out.println("Your scissors got smashed by my rock. You win!");
              }
} // terminates outer if

如果您还有其他问题,请对此答案发表评论!

答案 1 :(得分:0)

我不打算为你编写程序,而是要指出你的逻辑中的缺陷。

if (playerOne.equals("ROCK"))**{}** //notice you aren't actually executing any logic here?
     **else** if (playerTwo.equals("PAPER")){ //this line is being called if playerOne does **not** equal rock.
     System.out.println("Your rock got covered by my paper. You lose!");
}

同样的逻辑也适用于你的其他代码块。

相反它应该是

的内容
   if (playerOne.equals("ROCK")){
       if (playerTwo.equals("PAPER")){ 
         System.out.println("Your rock got covered by my paper. You lose!");
       }
    }        

答案 2 :(得分:0)

if (playerOne.equals("ROCK")){}
              else if (playerTwo.equals("PAPER")){
                 System.out.println("Your rock got covered by my paper. You lose!");
                 }
              else if (playerTwo.equals("SCISSORS")){
                 System.out.println("Your scissors got smashed by my rock. You win!");
                 }

您的条件块未正确写入。如果某个条件为真,则执行以下{}块。在你的代码中,你正在检查playerOne输入了什么,但是当输入匹配“ROCK”时你没有做任何事情。该块只是空白。

接下来将检查else if条件,(假设playerTwo.equals(“ROCK”))System.out.println(“你的摇滚....”);将仅基于playerOne的输入等于“ROCK”的事实执行。

您可能需要查看笔记,了解else if语句的工作方式和时间。

相反,你想要的是检查playerOne.equals(“ROCK”),然后检查该playerTwo.equals(“PAPER”)并做一些事情。

这适用于您正在检查playerOne输入的每种情况。