使用java小程序显示图像10卡洗牌器出现错误:(
我应该使用小程序来显示扑克牌的图像。小程序应该从您下载的“图像”文件夹中加载52张扑克牌图像。小程序应该洗牌(使用随机数生成器)并显示洗牌牌组的前10张牌。将牌分为两行,每行五张牌。 当我运行我的程序时,它会显示这个错误使用java小程序显示图像10卡洗牌器出现错误:(,java,image,compiler-errors,applet,awt,Java,Image,Compiler Errors,Applet,Awt,我应该使用小程序来显示扑克牌的图像。小程序应该从您下载的“图像”文件夹中加载52张扑克牌图像。小程序应该洗牌(使用随机数生成器)并显示洗牌牌组的前10张牌。将牌分为两行,每行五张牌。 当我运行我的程序时,它会显示这个错误 java.lang.ArrayIndexOutOfBoundsException: 10 at pkg12.NewApplet.init(NewApplet.java:69) at sun.applet.AppletPanel.run(AppletPanel.java:
java.lang.ArrayIndexOutOfBoundsException: 10
at pkg12.NewApplet.init(NewApplet.java:69)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:722)
我不知道为什么,所以我无法修复它
这是我的密码
package pkg12;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
public class NewApplet extends Applet
{
Image card1;
Image card2;
Image card3;
Image card4;
Image card5;
Image card6;
Image card7;
Image card8;
Image card9;
Image card10;
public void init()
{
Random random = new Random();
String cards[][] = {{"c1.gif", "c2.gif", "c3.gif", "c4.gif",
"c5.gif", "c6.gif", "c7.gif", "c8.gif", "c9.gif",
"c10.gif", "cj.gif", "ck.gif", "cq.gif"},
{"s1.gif", "s2.gif", "s3.gif", "s4.gif",
"s5.gif", "s6.gif", "s7.gif", "s8.gif",
"s9.gif", "s10.gif", "sj.gif", "sk.gif",
"sq.gif"}, {"d1.gif", "d2.gif", "d3.gif",
"d4.gif", "d5.gif", "d6.gif", "d7.gif",
"d8.gif", "d9.gif", "d10.gif", "dj.gif",
"dk.gif", "dq.gif"}, {"h1.gif", "h2.gif",
"h3.gif", "h4.gif", "h5.gif", "h6.gif",
"h7.gif", "h8.gif", "h9.gif", "h10.gif",
"hj.gif", "hk.gif", "hq.gif"}};
int selectedCard[] = new int[10];
int selectedSuit[] = new int[10];
int suit = random.nextInt(4);
int card = random.nextInt(13);
boolean newCard = false;
for(int x = 0; x < 10; x++)
{
while(newCard = false)
{
newCard = true;
suit = random.nextInt(4);
card = random.nextInt(13);
for(int y = 0; y < x; y++)
{
if(selectedCard[y] == card && selectedSuit[y] == suit)
{
newCard = false;
}
}
}
selectedCard[x] = card;
selectedSuit[x] = suit;
}
card10 = getImage(getDocumentBase(), cards[selectedSuit[10]][selectedCard[11]]);
card1 = getImage(getDocumentBase(), cards[selectedSuit[1]][selectedCard[1]]);
card2 = getImage(getDocumentBase(), cards[selectedSuit[2]][selectedCard[2]]);
card3 = getImage(getDocumentBase(), cards[selectedSuit[3]][selectedCard[3]]);
card4 = getImage(getDocumentBase(), cards[selectedSuit[4]][selectedCard[4]]);
card5 = getImage(getDocumentBase(), cards[selectedSuit[5]][selectedCard[5]]);
card6 = getImage(getDocumentBase(), cards[selectedSuit[6]][selectedCard[6]]);
card7 = getImage(getDocumentBase(), cards[selectedSuit[7]][selectedCard[7]]);
card8 = getImage(getDocumentBase(), cards[selectedSuit[8]][selectedCard[8]]);
card9 = getImage(getDocumentBase(), cards[selectedSuit[9]][selectedCard[9]]);
card10 = getImage(getDocumentBase(), cards[selectedSuit[10]][selectedCard[10]]);
}
public void paint(Graphics g)
{
g.drawImage(card1, 30, 30, this);
g.drawImage(card2, 30, 150, this);
g.drawImage(card3, 120, 30, this);
g.drawImage(card4, 120, 150, this);
g.drawImage(card5, 210, 30, this);
g.drawImage(card6, 210, 150, this);
g.drawImage(card7, 300, 30, this);
g.drawImage(card8, 300, 150, this);
g.drawImage(card9, 390, 30, this);
g.drawImage(card10, 390, 150, this);
}
}
包装pkg12;
导入java.applet.applet;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.util.Random;
公共类NewApplet扩展Applet
{
图像卡1;
图像卡2;
图像卡3;
图像卡4;
图像卡5;
图像卡6;
图像卡7;
图像卡8;
图像卡9;
图像卡10;
公共void init()
{
随机=新随机();
字符串卡[][]={{{“c1.gif”、“c2.gif”、“c3.gif”、“c4.gif”,
“c5.gif”、“c6.gif”、“c7.gif”、“c8.gif”、“c9.gif”,
“c10.gif”、“cj.gif”、“ck.gif”、“cq.gif”},
{“s1.gif”、“s2.gif”、“s3.gif”、“s4.gif”,
“s5.gif”、“s6.gif”、“s7.gif”、“s8.gif”,
“s9.gif”、“s10.gif”、“sj.gif”、“sk.gif”,
“sq.gif”}、{“d1.gif”、“d2.gif”、“d3.gif”,
“d4.gif”、“d5.gif”、“d6.gif”、“d7.gif”,
“d8.gif”、“d9.gif”、“d10.gif”、“dj.gif”,
“dk.gif”、“dq.gif”}、{“h1.gif”、“h2.gif”,
“h3.gif”、“h4.gif”、“h5.gif”、“h6.gif”,
“h7.gif”、“h8.gif”、“h9.gif”、“h10.gif”,
“hj.gif”、“hk.gif”、“hq.gif”};
int selectedCard[]=新int[10];
int selectedSuit[]=新int[10];
int suit=随机。nextInt(4);
int card=random.nextInt(13);
布尔值newCard=false;
对于(int x=0;x<10;x++)
{
while(newCard=false)
{
newCard=true;
套装=随机。下一套(4);
卡片=随机。nextInt(13);
对于(int y=0;y
您的selectedSuit
和selectedCard
是newint[10]
card10 = getImage(getDocumentBase(), cards[selectedSuit[10]][selectedCard[11]]);
你的数组大小是10所以你只能得到索引9,因为数组是从0开始的 请更改以下内容
card1 = getImage(getDocumentBase(), cards[selectedSuit[0]][selectedCard[0]]);
card10 = getImage(getDocumentBase(), cards[selectedSuit[9]][selectedCard[9]]);
现在它显示以下错误消息:java.lang.ArrayIndexOutOfBoundsException:11在赋值_12.init(赋值_12.java:81)在sun.applet.AppletPanel.run(AppletPanel.java:435)在java.lang.Thread.run(Thread.java:722)处