卡片小程序问题与变量。请帮忙(:

时间:2014-01-03 22:04:09

标签: java compiler-errors applet

我一直在研究一个applet,由于某种原因,它出错并且没有读取一些变量..我猜最后一个可能连接到第一组错误,但我不确定。谢谢(:我想解决这个问题。

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

public class Cards10 extends Applet
{
    Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10,      flipped;

public void init()
{
     String deckCards[] = {"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"};
    for(int k = 0; k < 10; k++)
    {
        Random rand = new Random();
    int r = rand.nextInt(52);
    card(k) =  getImage( getDocumentBase(), String.format("%ss.gif", deckCards[r]) ); //I know I'm using String.format wrong here..is there anyway I can get it to format a string and include the .gif as it is now or must I take the '.gif" out of my list and simply do %2s.gif or is there any other way to avoid that?
        }
 flipped = getImage(getDocumentBase(), "b1fv.gif");
}

public void paint(Graphics g)
{
    g.drawImage(flipped, 10, 10, this);
    for (int i = 1; i < 6; i++)
    {
         g.drawImage(card(k), 10 + (20*i), 10, this);
    }
    for (int j = 6; j < 11; j++) /*I know this shouldn't be here as it would draw each card j times (same for i)..fixed on mine but is there any way to do this without listing them all out?*/ 
    {
         g.drawImage(card(k), 10 + (20*j), 125, this);
    }
}

public int shuffle(String [] deckCards)
{
    for (int n = 0; n < 10; n++);
    {
         Random rand = new Random();
         int r = rand.nextInt(52);
         return r;
    }   
}
}

以下是错误:

   Main.java:26: error: cannot find symbol
             g.drawImage(card(k), 10 + (20*i), 10, this);
                              ^
  symbol:   variable k
  location: class Cards10
Main.java:30: error: cannot find symbol
             g.drawImage(card(k), 10 + (20*j), 125, this);
                              ^
  symbol:   variable k
  location: class Cards10

2 个答案:

答案 0 :(得分:0)

这正是你想要的。我们在聊天中讨论了原因。

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

public class Cards10 extends Applet
{

    String deckCards[] = {"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"};
    Random rand = new Random();
    Image[] card = new Image[10];


    public void init()
    {
        for(int k = 0; k < 11; k++){
            int r = rand.nextInt(51);
            card[k] = getImage(getDocumentBase(), deckCards[r]);
        }
        flipped = getImage(getDocumentBase(), "b1fv.gif");
    }

    public void paint(Graphics g)
    {
        g.drawImage(flipped, 10, 10, this);
        for(int i = 1;i<6;i++) g.drawImage(card[i], 10 + (40*i), 10, this);
        for(int i = 6;i<10;i++) g.drawImage(card[i], 10 + (40*(i-6)), 125, this);
    }
}

这产生了所需的结果。

答案 1 :(得分:0)

错误很明显。您没有尝试访问k变量。您需要使用正确的ij可执行文件

g.drawImage(flipped, 10, 10, this);
for (int i = 1; i < 6; i++)
{
     g.drawImage(card(k), 10 + (20*i), 10, this);  <-- proabably want to access i
}
for (int j = 6; j < 11; j++) /*I know this shouldn't be here as it would draw each card j times (same for i)..fixed on mine but is there any way to do this without listing them all out?*/ 
{
     g.drawImage(card(k), 10 + (20*j), 125, this); <-- probably want to use j
}

另外,

  1. 没有card()方法,除非我不只是看到。
  2. 也许您想要使用Image[]数组,因为这就是您尝试使用card(v)

    所做的事情
    Image[] cardImages = new Image[10];
    
  3. 然后你可以像这样初始化它们

    cardImages[k] =  getImage( ...