0

我最近一直想用 java 构建一个纸牌或二十一点游戏。当我开始编码时,我遇到了一个关于有效导入卡片的所有图像填充的问题。我立即查了一下,但没有得到任何答案。Wy 的问题是为什么你不能这样命名图像:

for (int num = 0; num1 < 40; num++ ){
    Image names[num] = getImage ( getDocumentBase (), "c" + (num +1) + ".gif" );
}

如果您发现任何其他问题,请告诉我。先感谢您。这是完整的代码:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;

public class CardsJavaProgram extends Applet {
public void init(){
    String names [ ] = { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10"};
    for (int num = 0; num1 < 40; num++ ){
        Image names[num] = getImage ( getDocumentBase (), "c" + (num +1) + ".gif" );
    }
}
public void paint( Graphics screen ){

    int x = 10;
    for (int num5 = 0; num5 < 5; num5++ ){
        screen.drawImage( names[ (int) (Math.random () * 39)], x, 10, 100, 100, this );
        x = x + 100;
    }
}
}

PS我知道Kings Queens和Jacks没有被进口。

4

1 回答 1

2

首先,您必须声明数组(在类级别使其可用于多个方法):

public class CardsJavaProgram extends Applet { 
    private Image[] images = new Image[40];

您还应该更改变量名称(例如images),因为您还有其他同名的变量。

然后可以填写数组:

for (int num = 0; num1 < 40; num++ ){   
    images[num] = //...
于 2012-10-21T03:21:30.497 回答