main方法不会在java中循环运行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了main方法不会在java中循环运行相关的知识,希望对你有一定的参考价值。

您好我正在用java编写一个文件,允许用户对计算机进行二十一点。以下是我的代码。每当我在Dr.javait中运行它时说运行main方法然而没有任何反应。我知道如果你不给一个循环一个条件来结束它是无限的但是我相信我已经在我的代码中给出了一个结束条件,但我可能会弄错。

public class BlackJack {

    public static void main(String [] args) {
        Scanner in = new Scanner(System.in);
        boolean playing = true;
        Random card = new Random();
        int playerCard1 = card.nextInt(11)+1;
        int playerCard2= card.nextInt(10)+1;
        int dealerCard1= card.nextInt(11)+1;
        int dealerCard2= card.nextInt(11)+1;
        int playerTotal= playerCard1 + playerCard2;
        boolean bust = true;
        boolean blackJack = true;
        while(playing) { // start while loop, loop until playing is false
            while((!blackJack) || (!bust)){ // begin while loop
                System.out.println("You drew a:" + playerCard1 + " and a " + playerCard2);
                System.out.println("Your total is:" + playerTotal);
                System.out.println("The dealer has a: " + dealerCard1 + "showing and a hidden card");
                if(playerTotal ==21) {
                    System.out.println("Your total is: " + playerTotal);
                    System.out.println("BLACKJACK! You WON!");
                    blackJack = true;
                    playing = false;
                }
                while (playerTotal <21){ // begin while loop
                    String playerDecision=in.next();
                    if(playerDecision.equals( "hit")) { // if player hits
                        int playerCard = card.nextInt(11)+1;
                        playerTotal = playerCard1 + playerCard2 + playerCard;
                        System.out.println("You drew a: " + playerCard);
                        System.out.println("Your total is now:" + playerTotal);
                    } else if (playerDecision.equals("Stay")) {
                        // player decides to stay 
                        System.out.println("Okay! It is now the dealer's turn");
                        System.out.println("His hidden card was:" + dealerCard2);
                        int dealerTotal = dealerCard1 + dealerCard2;
                        System.out.println("His total was: " + dealerTotal);
                        if(dealerTotal == 21) {
                            blackJack = true;
                            System.out.println("The dealer won");
                        } 
                        while (dealerTotal < 16) { // begin while for dealerTotal <16 
                            int dealerCard = card.nextInt(11)+1;
                            System.out.print("The dealer chose to hit");
                            System.out.println("He drew a:" + dealerCard);
                            dealerTotal = dealerCard1 + dealerCard2 + dealerCard;
                            System.out.println("His is total is: " + dealerTotal);
                        } // end while
                        if(dealerTotal >21) { // if dealer goes over 21 he busts, bust is true
                            System.out.println("The Dealer BUSTS! You WON!" );
                            bust = true;
                            playing = false;
                        } else if(dealerTotal >playerTotal) {
                            System.out.println("The Dealer Won! You lost");
                            blackJack = true;
                            playing= false;
                        } else if(playerTotal > dealerTotal) { 
                            System.out.println("You Won");
                            blackJack = true;
                            playing = false;
                        }
                    } // end else if player stays
                }
            }
        } // end while loop for player<21
    }
}
答案

看起来好像你正在将blackjack的布尔值初始化为true,但是你的第一个while循环只有在这个值为false时才会启动:!blackjack,简单的解决方法是更改​​它以便在类级别使blackjack = false。

...
boolean bust = true;
boolean blackJack = false;

while(playing) { // start while loop, loop until playing is false

    while((!blackJack) || (!bust)){ // begin while loop
...

以上是关于main方法不会在java中循环运行的主要内容,如果未能解决你的问题,请参考以下文章

怎么在java main方法里用一个线程的sleep()方法让一个循环执行一次后阻塞一秒再循环下一次

多线程

多线程

java自学之路-day17

java中哪些内容应该放在main函数中?

201555332盛照宗—网络对抗实验1—逆向与bof基础