当前位置: 代码迷 >> Java相关 >> 为什么这个程序就是运行不了?
  详细解决方案

为什么这个程序就是运行不了?

热度:123   发布时间:2006-10-09 21:33:39.0
为什么这个程序就是运行不了?

我实在是看不出问题,但是问题却是很大,请高手参详,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class random extends JApplet implements ActionListener
{
JLabel oneLabel,twoLabel,threeLabel,fourLabel,fiveLabel,
sixLabel,sevenLabel,eightLabel,nineLabel,tenLabel,
elevenLabel,travelLabel;
JTextField oneField,twoField,threeField,fourField,fiveField,
sixField,sevenField,eightField,nineField,tenField,
elevenField,travelField;
JButton DiceButton;

int Dice = 0;
int a[] = new int [13];

public void init ()
{
Container container = getContentPane();
container.setLayout ( new FlowLayout () );

oneLabel = new JLabel ( "出现1的次数:" );
container.add ( oneLabel );
oneField = new JTextField ( 10 );
container.add ( oneField ) ;
oneField.setEditable ( false );

twoLabel = new JLabel ( "出现2的次数:" );
container.add ( twoLabel );
twoField = new JTextField ( 10 );
container.add ( twoField );
twoField.setEditable ( false );

threeLabel = new JLabel ( "出现3的次数:" );
container.add ( threeLabel );
threeField = new JTextField ( 10 );
container.add ( threeField );
threeField.setEditable ( false );

fourLabel = new JLabel ( "出现4的次数:" );
container.add ( fourLabel );
fourField = new JTextField ( 10 );
container.add ( fourField );
fourField.setEditable ( false );

fiveLabel = new JLabel ( "出现5的次数:" );
container.add ( fiveLabel );
fiveField = new JTextField ( 10 );
container.add ( fiveField );
fiveField.setEditable ( false );

sixLabel = new JLabel ( "出现6的次数:" );
container.add ( sixLabel );
sixField = new JTextField ( 10 );
container.add ( sixField );
sixField.setEditable ( false );

sevenLabel = new JLabel ( "出现7的次数:" );
container.add ( sevenLabel );
sevenField = new JTextField ( 10 );
container.add ( sevenField ) ;
sevenField.setEditable ( false );

eightLabel = new JLabel ( "出现8的次数:" );
container.add ( eightLabel );
eightField = new JTextField ( 10 );
container.add ( eightField );
eightField.setEditable ( false );

nineLabel = new JLabel ( "出现9的次数:" );
container.add ( nineLabel );
nineField = new JTextField ( 10 );
container.add ( nineField );
nineField.setEditable ( false );

tenLabel = new JLabel ( "出现10的次数:" );
container.add ( tenLabel );
tenField = new JTextField ( 10 );
container.add ( tenField );
tenField.setEditable ( false );

elevenLabel = new JLabel ( "出现11的次数:" );
container.add ( elevenLabel );
elevenField = new JTextField ( 10 );
container.add ( elevenField );
elevenField.setEditable ( false );

travelLabel = new JLabel ( "出现12的次数:" );
container.add ( travelLabel );
travelField = new JTextField ( 10 );
container.add ( travelField );
travelField.setEditable ( false );

DiceButton = new JButton ( "开始" );
container.add ( DiceButton );
DiceButton.addActionListener ( this );
}

public void actionPerFormed ( ActionEvent actionevent )
{
for ( int i=0;i<3600;i++ )
{
Dice = sumDice ();
a[Dice]++;
}

display (a) ;
}

public int sumDice ( )
{
int die1 = 1 + ( int )( Math.random()*6 ),
die2 = 1 + ( int )( Math.random()*6 ),
sum = die1 + die2;

return sum;
}

public void display (int b[])
{
oneField.setText ( Integer.toString ( b[1] ) );
twoField.setText ( Integer.toString ( b[2] ) );
threeField.setText ( Integer.toString ( b[3] ) );
fourField.setText ( Integer.toString ( b[4] ) );
fiveField.setText ( Integer.toString ( b[5] ) );
sixField.setText ( Integer.toString ( b[6] ) );
sevenField.setText ( Integer.toString ( b[7] ) );
eightField.setText ( Integer.toString ( b[8] ) );
nineField.setText ( Integer.toString ( b[9] ) );
tenField.setText ( Integer.toString ( b[10] ) );
elevenField.setText ( Integer.toString ( b[11] ) );
travelField.setText ( Integer.toString ( b[12] ) );
}
}



搜索更多相关的解决方案: 运行  

----------------解决方案--------------------------------------------------------
什么问题?说清楚
----------------解决方案--------------------------------------------------------

好你就是说在引用了this之后,就出现了故障,我也说不清


----------------解决方案--------------------------------------------------------
不说清楚也没法帮你了
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class JRandom extends JApplet implements ActionListener
{
JLabel label[]=new JLabel[12];
JTextField field[]=new JTextField[12];
JButton DiceButton;
int Dice=0;
int a[] = new int [12];
public void init()
{
Container container=this.getContentPane();
container.setLayout(new FlowLayout());
for(int i=0;i<12;i++)
{
label[i]=new JLabel("出现"+(i+1)+"的次数:");
container.add(label[i]);
field[i]=new JTextField(12);
container.add(field[i]) ;
field[i].setEditable(false);
}
DiceButton=new JButton("开始");
container.add(DiceButton);
DiceButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==DiceButton)
{
for(int i=0;i<3600;i++)
{
Dice=sumDice();
a[Dice]++;
display(a) ;
}

}
}
public int sumDice()
{
int die1=(int)(Math.random()*6);
int die2=(int)(Math.random()*7);
int sum=die1+die2;

return sum;
}
public void display(int b[])
{
for(int i=0;i<12;i++)
{
field[i].setText(Integer.toString(b[i]));
}
}

}
//<applet code=copyTest width=300 height=200></applet>
兄台的代码我看着实在难受,所以就改了一下;
主要毛病就是获取随机数的时候数组a[]越界了,因为你是从2开始的;
还有一点就是,我喜欢用数组循环赋值,仅供参考,你也可以用你的方法,就是麻烦了点

----------------解决方案--------------------------------------------------------
  相关解决方案