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

标签 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

最佳答案

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

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);
    }
}

这恰好产生所需的结果。

关于java - 卡小程序问题与变量。请帮忙(:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20913509/

相关文章:

java - Google Play 游戏服务无法登录

java - 如何从 ASP .NET 网站检测安装在客户端上的 Java 运行时?

eclipse - 仅显示 Eclipse CDT 中的编译错误

java - Applet-Javascript 交互中的焦点行为

java - Java 6 上的签名 Applet

Java/JAI - 保存灰度图像

java - 当我使用 flutter 运行 native 代码时出现一些错误

android - 无法弄清楚 "cannot be provided without an @provides-annotated method"错误

C++: 'Foo' 中成员 'f' 的错误请求,它是非类类型 'Foo*'

java - 在线小程序给出 ClassNotFoundException