首页 文章

如何继续循环java

提问于
浏览
0

我的程序不会做我需要它做的事情,我已经坚持这个问题六个小时了 . 它提示用户输入他们想扔硬币的次数,该程序计算抛出的头部和尾部的数量,并吐出总头部和尾部以及作为头部滚动的百分比 . 我的问题是,循环只有在第一次通过时才会结束 . 它只是停止并且不会提示用户选择另一个号码或按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"));
        }
    }
}

4 回答

  • 0

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

  • 0
    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 + "]");
        }
    }
    
  • 0

    你需要一个循环来代替你的代码:while(c!= 0)

  • 0

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

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

    但是,如果numero == 0,则 for - j < numero 的条件为false,因此执行根本不会进入for循环 . 所以 break 永远不会运行 .

    你可能想要更像的东西:

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

    ...虽然这里写的是 if 是无关紧要的,因为 for 循环仍然会循环零次 .

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

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

    如果将其分解为更多方法,您会发现您的代码变得更加清晰 . 而不是将您的硬币投掷循环放在 if 语句中(这称为内联),将其放在自己的方法中,并在 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
    }
    

相关问题