【问题标题】:Java rock-paper-scissorsJava 石头剪刀布
【发布时间】: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\".");
   }
}

【问题讨论】:

    标签: java random


    【解决方案1】:

    在我弄清楚你的代码有什么问题之前,这里有一些建议:

    Math.round() 不是必需的。如果您要转换为int,那么它会自动四舍五入到最接近的整数。

    您应该声明一个具有三种结果的 Enum。然后您可以通过名称而不是数字或字符串来引用它们:

    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
    

    此外,cmets 使用两个 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!
    

    所以要解决您的问题,您的代码 sn-p 需要如下所示:

    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
    

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

    【讨论】:

    • 我问过别人这个问题,但我也会评论你的建议。
    • 感谢您的帖子!如果我再次得到这种分配,我肯定会考虑声明一个 Enum。别担心!正斜杠不在代码中,我只是在发帖时迅速冲了进去。现在我在我的最后一个陈述中遇到了麻烦......“其他......错误。重新启动”一个。我还在开篇文章中更新了我的代码。
    • 发生这种情况的原因是因为您没有正确链接 if else 语句。只有第一个如果你有(检查它是否相等的那个)应该保持一个if。其他一切都需要是“else if”,因为只有在前一个 if 不正确时它才会去那里。最后一次“错误重启”应该是一个 else,因为它只发生在前一个 if 检查为真时没有。
    【解决方案2】:

    我不会为你编写程序,而是指出你的逻辑缺陷。

    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!");
           }
        }        
    

    【讨论】:

    • 非常感谢!我把它修得很好,现在它可以工作了。我不确定如何处理代码的结尾(针对不是石头、纸或剪刀的输入)。当我尝试玩已编译的游戏时,它总是随机弹出。我还将更新后的代码放入我原来的主题帖子中。
    • @Tony,在您更新的代码中,if (playerOne.equals("PAPER") {} 之前缺少“else”。此外,您在最后的 else 之前缺少一个结束 }
    【解决方案3】:
    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("Your rock ....");将仅基于 playerOne 的输入等于“ROCK”这一事实执行。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2018-04-06
      • 1970-01-01
      • 2017-02-10
      • 2016-01-16
      • 1970-01-01
      相关资源
      最近更新 更多