【问题标题】:How to continue loop java如何继续循环java
【发布时间】:2014-02-18 13:09:26
【问题描述】:

我的程序不会做我需要它做的事情,我已经在这个问题上停留了六个小时。它提示用户输入他们想掷硬币的次数,程序会计算被抛的正面和反面的数量,并吐出正面和反面的总数以及正面朝上的百分比。我的问题是循环一旦第一次通过就结束了。它只是停止并且不会提示用户选择另一个数字或按 0 退出程序并返回主菜单,我该如何解决这个问题?另外,我怎样才能选择返回另一个循环。我已经尝试过 break 方法,但它对我不起作用。我不知道我真的迷路了。我怎样才能做到这一点,让这个循环继续抛硬币,仍然问用户他们想抛多少次?同时允许他们简单地按 0 并退出此循环以返回主菜单?我没有在此处发布该代码,但这是一个带有不同子选项/游戏的 while 循环。

if (anotherScanner.hasNext("t|T")) {

    c = anotherScanner.next();
    usersSelection = true;
    System.out.println("");
    System.out.println("COIN TOSS SIMULATOR");
    System.out.println("");
    System.out.println("Enter 0 to quit. How many tosses?");

    Random rand = new Random();
    float headsCount = 0;
    float tailsCount = 0;
    Scanner scanMan = new Scanner(System.in);
    int numero = scanMan.nextInt();
    boolean headsOrTails;

    for (int j=0;j < numero;j++) {
        if (numero == 0) { break; }
        headsOrTails = rand.nextBoolean();

        if (headsOrTails == true) {
            headsCount++;
        } else {
            tailsCount++;               

            System.out.println(headsCount + " heads and " + tailsCount + " tails means " + (headsCount/(headsCount+tailsCount)*100 + "% were heads"));
        }
    }
}

【问题讨论】:

    标签: java loops


    【解决方案1】:

    用户输入代码也需要在循环中。

    【讨论】:

      【解决方案2】:
      System.out.println("\ncontinue to inner when i is 2 and j is 2:");
      for (int i = 1; i <= 3; i++) {
          here4: for (int j = 1; j <= 3; j++) {
              if ((i == 2) && (j == 2)) {
                  System.out.print("[continue to inner]");
                  continue here4;
              }
              System.out.print("[i:" + i + ",j:" + j + "]");
          }
      }
      

      【讨论】:

        【解决方案3】:

        您需要一个循环来包含您的代码:while(c != 0)

        【讨论】:

        • 这个答案有点短。你可以把它写得更具体一些,也许会显示在哪里添加那个 while 循环。
        【解决方案4】:

        您的代码打算始终进入“for”循环,然后如果numero == 0,则离开循环。

         for (int j = 0; j < numero; j++) {
            if (numero == 0) {
                break;
            }
            ...
         }
        

        但是,如果numero == 0,则for——j &lt; numero的条件为假,所以执行根本不会进入for循环。这样break 将永远不会运行。

        你可能想要更多类似的东西:

        if(numero !=0) {
           for (int j = 0; j < numero; j++) {
              ...
           }
        }
        

        ...尽管如此处所述,if 无关紧要,因为numero==0 for 循环仍然迭代零次。

        但是你的 println() 是在循环内 - 每次抛出“tails”时它都会执行。我想你真的希望 println() 在所有硬币都被扔掉之后发生一次:

        if(numero !=0) {
           for (int j = 0; j < numero; j++) {
              ...
           }
           System.out.println(...);
        }
        

        你会发现,如果你把它分解成更多的方法,你的代码会变得更加清晰。与其将抛硬币循环放在if 语句中(这称为inline),不如将它放在它自己的方法中,然后在for 循环中调用该方法。所以你最终会得到这样的代码:

        // Get input; play game until input is zero.
        void playCoinTossGames(Scanner scanner) {
           int input = promptAndGetInt(scanner);
           while(input != 0) {
              playOneCointossGame(input);
              int input = promptAndGetInt(scanner);
           }
          // note two calls to get input above. This is called a
          // "read-before-read-while" loop
        }
        
        // Print a prompt, then scan input for an integer
        int promptAndGetInt(Scanner scanner) {
           System.out.println("How many tosses? (0 to exit)";
           return scanner.nextInt();
        }
        
        // Toss numberOfTosses times, then report the % of heads.
        void playOneCointossGame(int numberOfTosses) {
           for(int i=0; i<numberOfTosses; i++) {
               // Toss a coin and record result
           }
           System.out.println(...); // summary of results
        }     
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-05-25
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          • 2011-08-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多