【问题标题】:Java Conditions Confusion? (Beginner)Java条件混乱? (初学者)
【发布时间】:2015-05-30 12:54:55
【问题描述】:

这个程序的目的是制作一个石头剪刀布游戏。我已经成功地做到了,但是无论我尝试什么,我都无法让它循环。我试过了:

while (index = 0)
while (index < gamesCount)

但是,虽然我的索引为 0 且条件为 while (index != 0),但它似乎是运行程序的唯一条件,但无论如何它都不会循环。如何让我的游戏循环播放?

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Random randomGen = new Random();
        //Variables
        String player1;
        int cpu;
        int start = 1;
        int end = 3;
        int index = 0;

        // 1 = Rock | 2 = Scissors | 3 = Paper
        //Code
        System.out.println("Welcome to Rock, Paper, Scissors!");
        while (index != 0) {
            System.out.print("Rock, Paper, or Scissors?: ");
            player1 = in.nextLine();
            cpu = randomGen.nextInt(3);
            System.out.println(cpu);

            if (player1.equals("Rock") && (cpu == 2)) {
                    System.out.println("You lose!");
            } else if (player1.equals("Rock") && (cpu == 1)) {
                    System.out.println("You win!");
            } else if (player1.equals("Rock") && (cpu == 0)) {
                System.out.println("Draw!");
            }

            // --------------------

            if (player1.equals("Scissors") && (cpu == 2)) {
                    System.out.println("Draw!");
            } else if (player1.equals("Scissors") && (cpu == 1)) {
                    System.out.println("You win!");
            } else if (player1.equals("Scissors") && (cpu == 0)) {
                    System.out.println("You lose!");
            }

            //---------------------

            if (player1.equals("Paper") && (cpu == 2)) {
                    System.out.println("You lose!");
            } else if (player1.equals("Paper") && (cpu == 1)) {
                    System.out.println("You win!");
            } else if (player1.equals("Paper") && (cpu == 0)) {
                    System.out.println("Draw!");
            }
        }
    }
}

【问题讨论】:

  • while (index &lt; gamesCount) 似乎是一个不错的选择 - 为什么它没有达到你想要的效果?如果您使用这种方法,您需要在每个循环(即循环内的index++)的索引中添加一个(increment),然后将gamesCount 定义为您的循环数想要(例如int gamesCount = 10;
  • P.S.目前在您的程序中,index 始终为零,因此 index != 0 将始终为 false,因此程序永远不会进入该代码块。当您尝试while (index = 0) 时,您可能意味着while(index == 0) - which would make the game loop infinitely at the moment. You need to understand that =` 是Java 中的赋值运算符(即给变量一个值),== 是相等比较运算符(即测试两个值相同)。

标签: java loops conditional-statements


【解决方案1】:

两个错误:

while (index != 0);

这是整个循环。它在 { } 块(您没有)的末尾或在第一个 ;紧接在语句之后。

不过,更正一下,它仍然不会循环:

int index = 0;

        // 1 = Rock | 2 = Scissors | 3 = Paper



        //Code
        System.out.println("Welcome to Rock, Paper, Scissors!");
        while (index != 0);

index = 0,因此 (index != 0) 永远不会返回 true。

【讨论】:

    【解决方案2】:

    您的索引变量设置为值 0。 你的 while 循环说

    while (index != 0);
    

    这意味着,当索引不为 0 时,运行我的代码。问题是您的代码将永远不会运行,因为您的索引值始终为 0。 尝试将其更改为另一个值(例如 5),它现在应该可以工作了。

    :)

    【讨论】:

      【解决方案3】:

      您将索引变量设置为 0。while 循环的条件是,如果索引不等于 0,则执行循环中的代码。由于您的索引等于 0,因此不会执行循环中的指令。此外,您需要更新循环中的索引变量,以便如果满足您要查找的条件,代码将停止循环。 即:

      int gamesPlayed = 0;
      int gamesRequested = 3; // or get this from the user
      
      while (gamesPlayed < gamesRequested){
          String player1Choice = in.nextLine();
          if(!"".equals(player1)){
          // your code
              gamesPlayed++;
          } else {
              System.out.print("Rock, Paper, or Scissors?: ");
          }
      }
      

      【讨论】:

      • 我做到了!谢谢!但现在唯一的问题是,当我运行它并输入我想玩 3 场游戏的内容时……它第一次询问您的选项时,它说了两次“Rock, Paper or Scissors: Rock, Paper or Scissors:”
      • 当玩家输入他们想玩的游戏数量时,您的代码会生成一个随机数,然后将其打印出来。扫描仪正在拾取该打印输出,然后您的条件没有得到满足,因此循环将再次执行。我会在进入循环后测试您的扫描仪的结果,看看它是否具有您正在寻找的价值,如果没有,请询​​问石头、纸或剪刀。即,类似于上面编辑的答案...
      猜你喜欢
      • 2014-05-04
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 2014-05-05
      相关资源
      最近更新 更多