为什么这个循环循环代码但没有执行正确的操作?

时间:2016-09-30 18:00:56

标签: java

当我点击这个代码的重试时,它会工作并询问循环翻转硬币的次数,但只是打印“翻转硬币”并且什么都不做。谁知道怎么修它?我认为错误可能来自X已经少于numloop但我不知道如何解决它。

这是我的代码:

import java.util.Scanner;

public class coinFlip {

  public static void main (String[]args)throws InterruptedException {

    Scanner sc = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);
    int numloop;
    int x = 0;
    String choice;
    Boolean bool = true;


    while (bool=true){
      System.out.println("How Many Coins Would You Like To Flip?");
      numloop = sc.nextInt();

      if (numloop == 13 || (numloop == 5 || (numloop == 8 || (numloop == 666)))) {
        System.out.println("ILLUMINATI CONFIRMED ??????");
        System.out.println();
      }

      System.out.println("Flipping Coin(s)...");
      System.out.println();

      while (x<numloop) {

        int rng = (int)(Math.random()*10+1);

        if (rng <= 5) {

          System.out.println("You Flipped Heads");
        }

        else {
          System.out.println("You Flipped Tails");
        }

        x=x+1;
      }


      System.out.println();
      System.out.println("Would You Like To 'Quit' Or 'Retry'?");
      choice = scan.nextLine();

      if (choice.equalsIgnoreCase("Quit")) {
        System.out.println ("Have A Nice Day");
        Thread.sleep(1000);
        System.exit(0);
      }

      if (choice.equalsIgnoreCase("Retry")) {
        bool=true;
      }


    }
  }
}

非常感谢你!

2 个答案:

答案 0 :(得分:4)

您永远不会在循环中重新初始化x,因此它仍然等于numLoop

x = 0添加到外部循环的顶部,每次用户说重试时它都会重置该值。

此外,虽然在这种情况下无关紧要,但这一行是错误的:

while (bool=true){

这会将true分配给bool,并始终会继续循环。您通常应该说while (bool),但由于您从未将其设置为false,因此您可以改为while (true)

答案 1 :(得分:0)

如果您将int x=0从最初的while循环外移到其中,则您不会遇到此问题。每次用户重试时它都会重置。

 Scanner sc = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);
    int numloop;

    String choice;
    Boolean bool = true;


    while (bool=true){
        int x = 0;
        System.out.println("How Many Coins Would You Like To Flip?");
        numloop = sc.nextInt();

        if (numloop == 13 || (numloop == 5 || (numloop == 8 || (numloop == 666)))) {
            System.out.println("ILLUMINATI CONFIRMED ??????");
            System.out.println();
        }

        System.out.println("Flipping Coin(s)...");
        System.out.println();

        while (x<numloop) {

            int rng = (int)(Math.random()*10+1);

            if (rng <= 5) {

                System.out.println("You Flipped Heads");
            }

            else {
                System.out.println("You Flipped Tails");
            }

            x=x+1;
        }


        System.out.println();
        System.out.println("Would You Like To 'Quit' Or 'Retry'?");
        choice = scan.nextLine();

        if (choice.equalsIgnoreCase("Quit")) {
            System.out.println ("Have A Nice Day");
            Thread.sleep(1000);
            System.exit(0);
        }

        if (choice.equalsIgnoreCase("Retry")) {
            bool=true;
        }


    }
}