问题描述
程序实现不了猜数字的过程,可能是TestThread的run方法靠MyWin的run实现,请问怎么改写importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.lang.*;publicclassNumberTest{publicstaticvoidmain(String[]args){MyWinwin=newMyWin();win.firstThread.start();win.secondThread.start();}}classMyWinextendsJFrameimplementsRunnable{TestThreadfirstThread;TestThreadsecondThread;JTextAreatextArea;Containercon;publicMyWin(){firstThread=newTestThread();secondThread=newTestThread();textArea=newJTextArea(8,28);con=getContentPane();con.setLayout(newFlowLayout());con.add(newJScrollPane(textArea));setBounds(100,100,150,150);setVisible(true);//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidrun(){if(Thread.currentThread()==firstThread){intrealNumber=(int)(Math.random()*100)+1;try{firstThread.dataOut.writeUTF("给你一个1到100之间的数,请您猜");while(true){intreceiveGuess=firstThread.dataIn.readInt();if(receiveGuess>realNumber){firstThread.dataOut.writeUTF("您猜大了");textArea.append("n您的猜测是:"+receiveGuess);}elseif(receiveGuess<realNumber){firstThread.dataOut.writeUTF("您猜小了");textArea.append("n您的猜测是:"+receiveGuess);}else{firstThread.dataOut.writeUTF("您猜对了");textArea.append("n您的猜测是:"+receiveGuess);return;}try{firstThread.sleep(1200);}catch(InterruptedExceptione){}}}catch(IOExceptione){}}if(Thread.currentThread()==secondThread){intgessNumber=(int)(Math.random()*100)+1;intmin=0,max=100;while(true){try{Stringmessage=secondThread.dataIn.readUTF();if(message.equals("给你一个1到100之间的数,请您猜")){secondThread.dataOut.writeInt(gessNumber);textArea.append(("ttttt我猜这个数是"+gessNumber));}elseif(message.equals("您猜大了")){max=gessNumber;gessNumber=(gessNumber+min)/2;secondThread.dataOut.writeInt(gessNumber);textArea.append(("ttttt我猜这个数是"+gessNumber));}elseif(message.equals("您猜小了")){min=gessNumber;gessNumber=(gessNumber+max)/2;secondThread.dataOut.writeInt(gessNumber);textArea.append(("ttttt我猜这个数是"+gessNumber));}else{textArea.append(("ttttt我猜对了,这个数是"+gessNumber));return;}}catch(IOExceptione){}}}}}classTestThreadextendsThread{PipedInputStreamin;DataInputStreamdataIn;PipedOutputStreamout;DataOutputStreamdataOut;publicTestThread(){in=newPipedInputStream();dataIn=newDataInputStream(in);out=newPipedOutputStream();dataOut=newDataOutputStream(out);}publicPipedInputStreamgetPipedInputStream(){returnin;}publicPipedOutputStreamgetPipedOutputStream(){returnout;}}