小程序中的主方法替换



我是一名新程序员,最近刚开始使用小程序。然而,我仍然困惑于将我通常会放在应用程序主方法中的代码放在哪里。目前,我正在编写一个程序,可以从普通牌组中随机出10张牌。

import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;
import java.lang.Math;
public class unit12Assignment extends Applet
{
Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13;
Image card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26;
Image card27, card28, card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39;
Image card40, card41, card42, card43, card44, card45, card46, card47, card48, card49, card50, card51, card52;
public void init()
{
    card1 = getImage( getDocumentBase(), "c1.gif" );
    card2 = getImage( getDocumentBase(), "c2.gif" );
    card3 = getImage( getDocumentBase(), "c3.gif" );
    card4 = getImage( getDocumentBase(), "c4.gif" );
    card5 = getImage( getDocumentBase(), "c5.gif" );
    card6 = getImage( getDocumentBase(), "c6.gif" );
    card7 = getImage( getDocumentBase(), "c7.gif" );
    card8 = getImage( getDocumentBase(), "c8.gif" );
    card9 = getImage( getDocumentBase(), "c9.gif" );
    card10 = getImage( getDocumentBase(), "c10.gif" );
    card11 = getImage( getDocumentBase(), "cj.gif" );
    card12 = getImage( getDocumentBase(), "cq.gif" );
    card13 = getImage( getDocumentBase(), "ck.gif" );
    card14 = getImage( getDocumentBase(), "d1.gif" );
    card15 = getImage( getDocumentBase(), "d2.gif" );
    card16 = getImage( getDocumentBase(), "d3.gif" );
    card17 = getImage( getDocumentBase(), "d4.gif" );
    card18 = getImage( getDocumentBase(), "d5.gif" );
    card19 = getImage( getDocumentBase(), "d6.gif" );
    card20 = getImage( getDocumentBase(), "d7.gif" );
    card21 = getImage( getDocumentBase(), "d8.gif" );
    card22 = getImage( getDocumentBase(), "d9.gif" );
    card23 = getImage( getDocumentBase(), "d10.gif" );
    card24 = getImage( getDocumentBase(), "dj.gif" );
    card25 = getImage( getDocumentBase(), "dq.gif" );
    card26 = getImage( getDocumentBase(), "dk.gif" );
    card27 = getImage( getDocumentBase(), "h1.gif" );
    card28 = getImage( getDocumentBase(), "h2.gif" );
    card29 = getImage( getDocumentBase(), "h3.gif" );
    card30 = getImage( getDocumentBase(), "h4.gif" );
    card31 = getImage( getDocumentBase(), "h5.gif" );
    card32 = getImage( getDocumentBase(), "h6.gif" );
    card33 = getImage( getDocumentBase(), "h7.gif" );
    card34 = getImage( getDocumentBase(), "h8.gif" );
    card35 = getImage( getDocumentBase(), "h9.gif" );
    card36 = getImage( getDocumentBase(), "h10.gif" );
    card37 = getImage( getDocumentBase(), "hj.gif" );
    card38 = getImage( getDocumentBase(), "hq.gif" );
    card39 = getImage( getDocumentBase(), "hk.gif" );
    card40 = getImage( getDocumentBase(), "s1.gif" );
    card41 = getImage( getDocumentBase(), "s2.gif" );
    card42 = getImage( getDocumentBase(), "s3.gif" );
    card43 = getImage( getDocumentBase(), "s4.gif" );
    card44 = getImage( getDocumentBase(), "s5.gif" );
    card45 = getImage( getDocumentBase(), "s6.gif" );
    card46 = getImage( getDocumentBase(), "s7.gif" );
    card47 = getImage( getDocumentBase(), "s8.gif" );
    card48 = getImage( getDocumentBase(), "s9.gif" );
    card49 = getImage( getDocumentBase(), "s10.gif" );
    card50 = getImage( getDocumentBase(), "sj.gif" );
    card51 = getImage( getDocumentBase(), "sq.gif" );
    card52 = getImage( getDocumentBase(), "sk.gif" );

}
public void paint(Graphics g)
{
    String card[] = new String[10];
    getCards( card[] );
    g.drawImage( cardSelection[0], 10, 10, this);
    g.drawImage( cardSelection[1], 90, 10, this);
    g.drawImage( cardSelection[2], 170, 10, this);
    g.drawImage( cardSelection[3], 250, 10, this);
            //unfinished
}

}

在那里的某个地方,我想添加一个代码,将随机卡放入一个数组中,并允许绘制方法完成它的工作。我有这样的代码。

String cardNumber; 
    double cardRandom;
    int cardRandomNumber;
    for ( int number = 0; number <  10; number++ );
    {
        cardRandom = Math.random() * Math.random() * 10;
        if ( cardRandom <= 52 );
        {
            cardRandomNumber = (int) Math.round( cardRandom );
        }
        cardNumber =  "card" + cardRandomNumber;
        cardSelection[number] = cardNumber;
    }

那么,我应该把通常放在应用程序主方法中的代码放在哪里呢?我想我应该把它放在一个方法中,但要确保我所说的。任何见解都值得赞赏。

您的卡设置代码属于小程序init()方法,因为您只初始化过一次。您可以将其提取到辅助方法中。

您应该重写start()方法并将代码放在那里。

附言:我强烈建议你学习数组。当您有许多变量的名称只有一个数字后缀不同时,您会发现数组很可能更适合解决问题。在这种特殊情况下,您很可能会将init()方法中的52行减少到少于10行。

相关内容

  • 没有找到相关文章

最新更新