询问用户是否想再玩一次

时间:2013-10-05 21:55:55

标签: java loops poker

我写了一个扑克计划,将卡片交给输入数量的玩家,然后处理房卡。我想知道如果玩家想要再玩一次并将程序放入循环中,该怎么问。因此,如果他们输入“是”,则程序重新启动,但如果他们输入“否”,则程序结束。我该怎么做?

import java.io.*;

public class Dealer {

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

        BufferedReader in;
        int x;
        String playerx;

        in = new BufferedReader(new InputStreamReader(System.in));
        System.out
                .println("Welcome to the Casino! My name is Zack and I'm going to be dealing your table. How many players are playing?");
        playerx = in.readLine(); // user input for menu selection
        x = Integer.valueOf(playerx).intValue();

        while (x >= 1 && x <= 24) {

            // create a deck of 52 cards and suit and rank sets
            String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
            String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "Jack", "Queen", "King", "Ace" };

            // initialize variables
            int suits = suit.length;
            int ranks = rank.length;
            int n = suits * ranks;
            // counter (5 house cards and 2 cards per player entered)
            int m = 5 + (x * 2);

            // initialize deck
            String[] deck = new String[n];
            for (int i = 0; i < ranks; i++) {
                for (int j = 0; j < suits; j++) {
                    deck[suits * i + j] = rank[i] + " of " + suit[j];

                }
            }

            // create random 5 cards
            for (int i = 0; i < m; i++) {
                int r = i + (int) (Math.random() * (n - i));
                String t = deck[r];
                deck[r] = deck[i];
                deck[i] = t;
            }

            // print results
            for (int i = 0; i < m; i++) {
                System.out.println(deck[i]);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

一石二鸟:在任何一只手发出之前,让运行该程序的人能够退出,以防他们不想玩。你已经有了这个结构。

while(1) {
    System.out.println("Welcome to ... How many players are playing (1-24) or enter 0 to exit?");
    x = Integer.valueOf(playerx).intValue();
    if(x == 0 || x >= 24) {
        break;
    }
    // rest of your logic remains.....
}

答案 1 :(得分:0)

我认为你之前没有编程经验吗?我建议你阅读发现的太阳文档here。在考虑添加再次播放选项之前,请先了解变量方法对象构造函数。如果你是初学者,Youtube教程也可以帮助你,但不要只依赖它们。