簡體   English   中英

Java while循環不會中斷(我也將條件設置為false)

[英]Java while loop won't break (I've set my condition to false, too)

我做了一些可以擲五個骰子的東西,直到獲得五個骰子。 我將在下面粘貼我的代碼,以便您可以看到它。 如果成功五次,我將while條件設置為false。 說“正在滾動...”之后,什么都沒有發生。 我應該使用.equals()嗎?

package omega;

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class One {

    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        Scanner scan = new Scanner(System.in);

        int one = 1+random.nextInt(6);
        int two = 1+random.nextInt(6);
        int three = 1+random.nextInt(6);
        int four = 1+random.nextInt(6);
        int five = 1+random.nextInt(6);

        boolean rolling = true;
        int rollCount = 0;

        System.out.println("====================");
        System.out.println("      Yahtzee!      ");
        System.out.println("====================");
        System.out.println("");


            System.out.println("Type 0 to roll.");
            int roll = scan.nextInt();

            System.out.println("Rolling...");

            while(rolling == true) {
                switch(roll) {
                case 0 :
                    if(two == one && three == one && four == one) {
                        rollCount++;
                        rolling = false;
                    }
                    else {
                        rollCount++;
                    }
                    break;
                default :
                    System.out.println("Invalid roll!");
                }
            }

            System.out.println("Yahtzee!");
            System.out.println("Took " + rollCount + " rolls!");



    }


}

您無需在循環中重新滾動。 如果roll不為0,則將是一個無限循環,並顯示“ Invalid roll!”。 一遍一遍又一遍。

幾乎沒有什么東西可以導致無限循環

  1. 第一個是您的roll變量始終為零,您不會再次滾動或不更改該變量。
  2. 第二個原因是五個變量通過if條件的可能性較小。

if(two == one && three == one && four == one) { rollCount++; rolling = false; }

另外,您不需要使用while(rolling == true)而可以使用while(rolling)因為您的rolling變量已初始化為true

這是我想出的

            while(rolling) {
                switch(roll) {
                case 0 :
                    if(two == one && three == one && four == one) {
                        rollCount++;
                        rolling = false;
                    }
                    else {
                        rollCount++;
                        System.out.println("If you want to stop roll press -1: ");
                        System.out.println("Or type 0 to roll again.");
                        roll = scan.nextInt();

                        if(roll == -1){
                            System.out.println("Yahtzee!");
                            System.out.println("Took " + rollCount + " rolls!");
                            return;
                        }
                    }
                    break;
                default :
                    System.out.println("Invalid roll!");
                    roll = 0;
                }
            }

            System.out.println("Yahtzee!");
            System.out.println("Took " + rollCount + " rolls!");

如果roll不為0,則需要將rolling更改為false。

default :
    System.out.println("Invalid roll!");
    rolling = false;      

因此,當您的while循環重新運行時,它將注意到false情況並中斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM