【问题标题】:Java - BlackJack project - Problems with I/O and cyclesJava - BlackJack 项目 - I/O 和周期问题
【发布时间】:2018-04-21 17:13:37
【问题描述】:

我正在尝试制作一个由字符输入驱动的简单BlackJack 游戏,但在后面的部分中我遇到了很多问题。

我评论了给我带来麻烦的部分,其余部分似乎没有错误,我对它进行了单元测试。 那么,我做了什么?我创建了一个类来保存抽出的牌并管理它们,牌桌、玩家和庄家都是牌桌的实例。 一个表最多有 5 张卡片(为简单起见),每个卡片对象都来自 Card 类,该类具有向卡片对象添加数据的方法。

主类驱动程序,并通过键盘输入的字符做出决定,此时我遇到了问题。

    import java.io.IOException;

class Table{
    Card[] hand = new Card[5];
    int counter = 1;

    Table() {
        for ( int i=0; i<hand.length; i++) {
            hand[i]=new Card();
        }
        hand[0].GetCard();


    }

    void ReadCards(){
        for(int i= 0;i<counter;i++ ) {
            System.out.println("The card "+(i+1)+" is " + hand[i].name + " "+ hand[i].seed + "."  );

        }
    }
    void DrawCards() {
        hand[counter].GetCard();
        counter++;
    }
    boolean isOut() {
        int sum = 0;
        for(int i= 0;i<counter;i++ ) {
            sum += hand[i].value; 
            if(sum >21) {
                return true;
            }

        }
        return false;
    }

    int TheSum() {
        int sum = 0;
        for(int i= 0;i<counter;i++ ) {
            sum += hand[i].value; 


        }
        return sum;
    }

    boolean isWIN(Table p2) {
        int sum = 0;
        int sump2 = 0;
        for(int i= 0;i<counter;i++ ) {
            sum += hand[i].value; 


        }
        for(int i= 0;i<p2.counter;i++ ) {
            sump2 += p2.hand[i].value; 


        }
        if (sum>sump2) {
            return true;
        }
        else return false;
    }



    class Card {
        public int value = 0;
        public String name = "";
        public String seed = ""; 



        void GetCard(){
            int positive = 0;
            do {
                positive = (int) (Math.random()*100) % 10;
            }while(positive == 0 );
            value = positive;
            if(value<10) {
                name = String.valueOf(value);
            }
            else {
                positive = 0;
                do {
                    positive = (int) (Math.random()*100) % 3;
                }while(positive == 0 );
                switch(positive) {
                case 1:
                    name = "J";
                    break;
                case 2:
                    name = "Q";
                    break;
                case 3:
                    name ="K";
                    break;

                }


            }
            positive = 0;
            do {
                positive = (int) (Math.random()*100) % 4;
            }while(positive == 0 );
            switch(positive) {
            case 1:
                seed = "CLUB";
                break;
            case 2:
                seed = "DIAMOND";
                break;
            case 3:
                seed ="SPADE";
                break;
            case 4:
                seed ="HEART";
                break;
            }

        }
    }
}



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


        System.out.println("Welcome to the BlackJack's table! Press y to start! ");
        char flag;
        do {
        flag = (char)System.in.read();

        }while(flag != 'y' );
        Table dealer = new Table();
        Table player = new Table();
        System.out.println("DEALER");
        dealer.ReadCards();
        System.out.println("PLAYER");
        player.ReadCards();
        flag = ' ';
        System.out.println("Do you want to draw a card? I'll draw until you'll press n");
/*
        flag = (char)System.in.read();
        while(flag != 'n' ) {
            player.DrawCards();
            player.ReadCards();
            if (player.isOut()) {
                System.out.println("YOU LOSE");
                System.exit(0);
            }
            flag = (char)System.in.read();
        }
        System.out.println("The dealer will draw");
        while(dealer.TheSum()<18) {
            dealer.DrawCards();
            dealer.ReadCards();
            if (dealer.isOut()) {
                System.out.println("YOU WIN");
                System.exit(0);
            }
        }
*/
        System.out.println("WHO WON?");

        if (player.isWIN(dealer)){
            System.out.println("YOU WIN");
        }
        else System.out.println("YOU LOSE");

    }
}

是的,我不习惯 java。 Console screenshot of the output here!

【问题讨论】:

  • 只是一个问题,这是作业吗?或用于个人学习。
  • 个人学习,我多年来一直避免使用java,一周前我买了一本书,我在这里。我主要想知道我是否使用了错误的 I/O 方法,因为它们太多了,我无法弄清楚为什么它只是在我启动 cmets 的地方跳过了我的请求。我已经缺少 c++、c#、python。
  • 如果代码太多,我可以询问有关 I/O 的问题,但扫描仪无法工作,在按下 'y' 后让我陷入无限循环,bufferedinputstream 具有相同的效果。这些是我看到的建议。

标签: java io blackjack


【解决方案1】:

这是一个解决循环问题的示例,但是游戏逻辑可能存在一些问题,这超出了本问题的范围。

   public static void main(String args[]) throws IOException {
        Scanner s = new Scanner(System.in);
        System.out.println("Welcome to the BlackJack's table! Press y to start! ");
        char flag;
        do {
            flag = (char) s.nextLine().charAt(0);

        } while (flag != 'y');
        Table dealer = new Table();
        Table player = new Table();
        System.out.println("DEALER");
        dealer.ReadCards();
        System.out.println("PLAYER");
        player.ReadCards();
        flag = ' ';
        System.out.println("Do you want to draw a card? I'll draw until you'll press n");

        while (true) {
            flag = s.nextLine().charAt(0);

            if (flag == 'n') {
                break;
            }

            player.DrawCards();
            player.ReadCards();
            if (player.isOut()) {
                System.out.println("YOU LOSE");
                System.exit(0);
            }
            System.out.println("The dealer will draw");
            while (dealer.TheSum() < 18) {
                dealer.DrawCards();
                dealer.ReadCards();
                if (dealer.isOut()) {
                    System.out.println("YOU WIN");
                    System.exit(0);
                }
            }

            System.out.println(String.format("The dealer has finished drawing. Dealers score %d. Your score %d", dealer.TheSum(), player.TheSum()));
        }

        System.out.println("WHO WON?");

        if (player.isWIN(dealer)) {
            System.out.println("YOU WIN");
        } else {
            System.out.println("YOU LOSE");
        }
    }

输出:

Welcome to the BlackJack's table! Press y to start! 
y
DEALER
The card 1 is 8 CLUB.
PLAYER
The card 1 is 2 SPADE.
Do you want to draw a card? I'll draw until you'll press n
n
WHO WON?
YOU LOSE

【讨论】:

  • 是的,可能是游戏出了点问题,毕竟我自己也不太了解这个游戏,这只是我当时想的比较简单的事情。程序还是那个循环的问题,如果我按下 n 以外的其他东西它崩溃, n 跳庄家转。我想知道为什么输入字符这么难。
  • 嗯,你必须输入一个字符,如果你只是按下回车,你会得到索引超出范围,因为它使用 charAt(0),它会查看第一个字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多