如何为这个二十一点游戏添加可重玩性?

时间:2016-01-31 05:06:31

标签: java

    import java.util.*;
import java.util.Scanner;

public class Assignment2 {
    public static void main(String args[]){ 
        Scanner stdin = new Scanner(System.in);

        Random random = new Random();     
        int ran2 = (random.nextInt(10));
        int ran1 = (random.nextInt(10));
        int total = ran1 + ran2;
        char exit = 'y';

        System.out.println("First cards: " + ran1 + ", " + ran2);
        System.out.println("Total: " + total);

        while(exit != 'n' && total < 21){
            System.out.println("Do you want another card? (y/n): ");
            exit = stdin.next().charAt(0);

            if (exit =='n'){
                System.out.println("Would you like to play again? (y/n): ");
                exit = stdin.next().charAt(0);                       
                if(total > 21 || exit == 'n'){          
                    break;
                }
            }

            int next = random.nextInt(10);
            System.out.println("Card: "+ next);
            total = total + next;//adds 
            System.out.println("Total: "+ total);

            if(total > 21){
                System.out.println("Bust.");
                break;
            }

            if(total == 21){
                System.out.println("You win!");
                break;
            }

        }   


    }
}

我让游戏正常运行,但是如果我想从头开始玩到你拿到第一张卡的地方,我该怎么做?在你赢或输之后如何让它重新开始游戏?我一直试图解决这个问题,仍然无法找到它是如何做到的

1 个答案:

答案 0 :(得分:2)

将代码包装在另一个循环中:)

<style type="text/css">
table { page-break-inside:auto }
tr    { page-break-inside:avoid; page-break-after:auto }
thead { display:table-header-group }
tfoot { display:table-footer-group }
</style>