《Java2实用教程》课后习题参考答案 下载本文

this.name=name; } }

15.在下列程序的主线程mai n 方法中,又开始运行了几个线程? import java.awt.* ; import java.awt.e vent.*;

class Gxy extends Thread implements Runnable {

Frame f=new Frame(\

TextField text1=new TextField(20 ),text2=new TextField(20 ),text3=new TextField(20 ); double n=0,正面=0,反面=0,正立=0; Gxy() {

f.setLayou t(new FlowLayout( )); f.setSi ze ( 2 00,300); f.setVisib le(true); f.add(text 1); f.add(text 2); f.add(text 3); f.validate ();

f.addWind owListener(new WindowAda pter() {

public void windowClosi ng(WindowEvent e) {

System.exi t(0); } }); }

public void run() {

while(tru e) { n++;

double i=Math.ra ndom(); if(i<0.5) { 正面++;

text1.setT ext(\正面出现的频率:\正面/n);

}

else if(i==0.5) { 正立++;

text2.setT ext(\正立出现的频率:\正立/n); } else { 反面++;

text3.setT ext(\反面出现的频率:\反面/n); } try {

Thread.sle ep(200); }

catch(Exce ption e) {} } } }

public class E {

public static void main(Strin g args[]) {

Thread t=new Thread(new Gxy()); t.start(); } }