随机卡和显示10个Java小程序

时间:2015-07-29 01:34:56

标签: java loops random applet awt

在此作业中,您将使用小程序显示扑克牌的图像。小程序应从您下载的“images”文件夹中加载一副52张扑克牌图像。小程序应该洗牌(使用随机数发生器)并显示洗牌组的前10张牌。将卡分为两行,每行五张。

我的applet部分工作,但是当我运行applet viewer时,显示器只是同一张卡十次。这是我的代码:

package CTY;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

public class Exercise12 extends Applet {

    Image c1;
    Image c2;
    Image c3;
    Image c4;
    Image c5;
    Image c6;
    Image c7;
    Image c8;
    Image c9;
    Image c10;

    public void init() {
        Random random = new Random();
        String cards[][] = {
                { "cards/c1.gif", "cards/c2.gif", "cards/c3.gif", "cards/c4.gif", "cards/c5.gif", "cards/c6.gif",
                        "cards/c7.gif", "cards/c8.gif", "cards/c9.gif", "cards/c10.gif", "cards/cj.gif",
                        "cards/ck.gif", "cards/cq.gif" },
                { "cards/s1.gif", "cards/s2.gif", "cards/s3.gif", "cards/s4.gif", "cards/s5.gif", "cards/s6.gif",
                        "cards/s7.gif", "cards/s8.gif", "cards/s9.gif", "cards/s10.gif", "cards/sj.gif",
                        "cards/sk.gif", "cards/sq.gif" },
                { "cards/d1.gif", "cards/d2.gif", "cards/d3.gif", "cards/d4.gif", "cards/d5.gif", "cards/d6.gif",
                        "cards/d7.gif", "cards/d8.gif", "cards/d9.gif", "cards/d10.gif", "cards/dj.gif",
                        "cards/dk.gif", "cards/dq.gif" },
                { "cards/h1.gif", "cards/h2.gif", "cards/h3.gif", "cards/h4.gif", "cards/h5.gif", "cards/h6.gif",
                        "cards/h7.gif", "cards/h8.gif", "cards/h9.gif", "cards/h10.gif", "cards/hj.gif",
                        "cards/hk.gif", "cards/hq.gif" } };

        int Card[] = new int[10];
        int Suit[] = new int[10];
        int suit = random.nextInt(4);
        int card = random.nextInt(10);
        boolean newCard = false;

        for (int i = 0; i < 10; i++) {
            while (newCard == false) {
                newCard = true;
                suit = random.nextInt(4);
                card = random.nextInt(13);

                for (int j = 0; j < i; j++) {
                    if (Card[j] == card && Suit[j] == suit ) {
                        newCard = false;
                    }
                }
            }

            Card[i] = card;
            Suit[i] = suit;
        }

        c1 = getImage(getDocumentBase(),
                cards[Suit[0]][Card[0]]);
        c2 = getImage(getDocumentBase(),
                cards[Suit[1]][Card[1]]);
        c3 = getImage(getDocumentBase(),
                cards[Suit[2]][Card[2]]);
        c4 = getImage(getDocumentBase(),
                cards[Suit[3]][Card[3]]);
        c5 = getImage(getDocumentBase(),
                cards[Suit[4]][Card[4]]);
        c6 = getImage(getDocumentBase(),
                cards[Suit[5]][Card[5]]);
        c7 = getImage(getDocumentBase(),
                cards[Suit[6]][Card[6]]);
        c8 = getImage(getDocumentBase(),
                cards[Suit[7]][Card[7]]);
        c9 = getImage(getDocumentBase(),
                cards[Suit[8]][Card[8]]);
        c10 = getImage(getDocumentBase(),
                cards[Suit[9]][Card[9]]);
    }

    public void paint(Graphics graphics) {
        graphics.drawImage(c1, 30, 30, this);
        graphics.drawImage(c2, 30, 150, this);
        graphics.drawImage(c3, 120, 30, this);
        graphics.drawImage(c4, 120, 150, this);
        graphics.drawImage(c5, 210, 30, this);
        graphics.drawImage(c6, 210, 150, this);
        graphics.drawImage(c7, 300, 30, this);
        graphics.drawImage(c8, 300, 150, this);
        graphics.drawImage(c9, 390, 30, this);
        graphics.drawImage(c10, 390, 150, this);
    }
}

3 个答案:

答案 0 :(得分:2)

循环问题,因为newCard标志在一次迭代后总是为真

for (int i = 1; i <=10; i++) {
            while (newCard == false) {
                newCard = true;
                suit = random.nextInt(4);
                card = random.nextInt(13);

                for (int j = 0; j < i; j++) {
                    if (Card[j] == card && Suit[j] == suit ) {
                        newCard = false;
                    }
                }
            }

            Card[i] = card;
            Suit[i] = suit;
        }

答案 1 :(得分:2)

这是你的问题:

    for (int i = 0; i < 10; i++) {
        while (newCard == false) {
            newCard = true;
            suit = random.nextInt(4);
            card = random.nextInt(13);

            for (int j = 0; j < i; j++) {
                if (Card[j] == card && Suit[j] == suit ) {
                    newCard = false;
                }
            }
        Card[i] = card;
        Suit[j] = suit;
        }

    }

答案 2 :(得分:2)

问题在于你的循环设置:

for (int i = 0; i < 10; i++) {
    while (newCard == false) {
        newCard = true;
        suit = random.nextInt(4);
        card = random.nextInt(13);

        for (int j = 0; j < i; j++) {
            if (Card[j] == card && Suit[j] == suit ) {
                newCard = false;
            }
        }
    }

    Card[i] = card;
    Suit[i] = suit;
}

第一次通过while循环i为0,因此它永远不会进入内部for循环,导致newCard保持为true。因为newCard是真的,所以它不会再次通过while循环,因此变量card和suit永远不会通过外部for循环的迭代进行更改。

一种解决方案是在while循环之前设置newCard = false

for (int i = 0; i < 10; i++) {
    newCard = false;
    while (newCard == false) {
        newCard = true;
        suit = random.nextInt(4);
        ...

这将确保它始终在外部for循环的每次迭代中进入while循环。