package myproject;
import java.io.*;public class Exp2 {
  public Exp2() {
  }
  
  public static void main(String args[])
  {
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream();
    try
    {
      in.connect(out);
    }
    catch(IOException e)
    {
      System.out.println("异常:" + e.toString());
    }
    
    int i = 0;
    while(i < 10)
    {
      question q1 = new question(out,i);      //出错
      answer a1 = new answer(in);             //出错
      q1.start();
      a1.start();
      i++;
    }
  }
  
  class question extends Thread
  {
    PipedOutputStream out;
    int i;
    public question(PipedOutputStream out,int i)
    {
      this.out = out;
      this.i = i;
    }
    
    public void run()
    {
      try
      {
        out.write(i);
        sleep(1);
        System.out.println("Quesion" + i);
        out.close();
      }
      catch(InterruptedException e)
      {
        System.out.println("中断异常:" + e.toString());
      }
      catch(IOException e)
      {
        System.out.println("输入输出异常:" + e.toString());
      }
    }
  }
  
  class answer extends Thread
  {
    PipedInputStream in;
    public answer(PipedInputStream in)
    {
      this.in = in;
    }
    
    public void run()
    {
      try
      {
        int value  = in.read();
        sleep(1);
        System.out.println("Answer" + value);
        in.close();
      }
      catch(InterruptedException e)
      {
        System.out.println("中断异常:" + e.toString());
      }
      catch(IOException e)
      {
        System.out.println("输入输出异常:" + e.toString());
      }
    }
  }
}请大家帮我改改错啊

解决方案 »

  1.   

    最后一个“}”移到class question extends Thread前一行即可
      

  2.   

    import java.io.*;public class Exp2 {
        public Exp2() {
        }    public static void main(String args[]) {
            PipedInputStream in = new PipedInputStream();
            PipedOutputStream out = new PipedOutputStream();
            try {
                in.connect(out);
            } catch (IOException e) {
                System.out.println("异常:" + e.toString());
            }        int i = 0;
            while (i < 10) {
                question q1 = new question(out, i); // 出错
                answer a1 = new answer(in); // 出错
                q1.start();
                a1.start();
                i++;
            }
        }
    }
        class question extends Thread {
            PipedOutputStream out;
            int i;        public question(PipedOutputStream out, int i) {
                this.out = out;
                this.i = i;
            }        public void run() {
                try {
                    out.write(i);
                    sleep(1);
                    System.out.println("Quesion" + i);
                    out.close();
                } catch (InterruptedException e) {
                    System.out.println("中断异常:" + e.toString());
                } catch (IOException e) {
                    System.out.println("输入输出异常:" + e.toString());
                }
            }
        }    class answer extends Thread {
            PipedInputStream in;        public answer(PipedInputStream in) {
                this.in = in;
            }        public void run() {
                try {
                    int value = in.read();
                    sleep(1);
                    System.out.println("Answer" + value);
                    in.close();
                } catch (InterruptedException e) {
                    System.out.println("中断异常:" + e.toString());
                } catch (IOException e) {
                    System.out.println("输入输出异常:" + e.toString());
                }
            }
        }