问题描述
我写一个小程序,将随机挑选10张卡,并将其显示在屏幕上。不过,我收到一个错误的.class和;当我试图传递的String []所需的错误。谁能帮助?这是我的code:
I am writing an applet that will randomly pick 10 cards and show them on the screen. However, I am receiving a .class error and a ; needed error when I attempt to pass an String[]. Anyone help? This is my code:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.applet.Applet;
import java.lang.Math;
import java.util.Random;
public class unit12Assignment extends Applet
{
Image card1 ... card52;
public void init()
{
card1 = getImage( getDocumentBase(), "c1.gif" );
...
card52 = getImage( getDocumentBase(), "sk.gif" );
}
public void getCards()
{
String cardNumber;
double cardRandom;
int cardRandomNumber;
String[] cardSelection = new String[10];
Random ran = new Random();
for (int number = 0; number <= 9; )
{
cardRandom = ran.nextInt(52) + 1;
cardRandomNumber = (int) Math.round( cardRandom );
if ( cardRandomNumber > 0 && cardRandomNumber <= 52 )
{
cardNumber = "card" + cardRandomNumber;
number++;
}
}
paint( String[] cardSelection );
}
public void paint(Graphics g, String[] card)
{
setBackground( Color.green );
g.drawImage( card[0], 10, 10, this);
g.drawImage( card[1], 90, 10, this);
g.drawImage( card[2], 170, 10, this);
g.drawImage( card[3], 250, 10, this);
}
}
推荐答案
这行:
paint( String[] cardSelection );
应该是语法
paint( cardSelection );
您只需要一个变量之前写的类型(例如的String []
)当你第一次声明。从那里它可以只通过其名称被称为
You only need to write the type (for example String[]
) before a variable when you first declare it. From there it can just be referred to by its name.
我也注意到,油漆
需要图形
参数以及一个的String [ ]
,所以你需要通过在还有:
I also notice that paint
takes a Graphics
argument as well as a String[]
, so you'll need to pass that in as well:
Graphics g = getGraphicsSomehow();
paint(g, cardSelection);
编辑:查看使用 图形
与替代解决方案。
see Andrew Thompson's answer for a disclaimer on using Graphics
with an alternative solution.
这篇关于在Java小程序的.class错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!