主类如下:
//空战模拟类定义 
import java.util.*;
import java.text.SimpleDateFormat;public class AirWarSimulation extends Thread{
  private long seed;//伪随机种子
  private int numOfPlanez;//红军飞机数量
  private int speedOfPlanez;//红军飞机速度 
  private int speedOfMissile;//导弹速度
  private int numberOfMissile;//导弹数量
  private int distanceBetweenMissileAndPlanez;//导弹距离红军飞机之间的距离
  private boolean noMissile;//控制两种仿真情况
  private Random randomNum;//随机数
  //private RedArmyBase base1,base2;//红军基地
  Vector vectorOfPlanez;//存储机群的向量
  //ResultRecorder rr;
//构造函数
  public AirWarSimulation(){
    randomNum=new Random();
    //rr=new ResultRecorder();
  }public synchronized void wF(String str) 
{
    try {
      writer = new PrintWriter(new FileOutputStream(resultOfRun, true), true);
      writer.println(str);
      writer.close();
    }
    catch (IOException e) {
      System.out.println(e);
    }
  }
//run函数
public void run(){
  vectorOfPlanez=new Vector();
  int num=numOfPlanez;
//当机群数量不为0时开始模拟飞行,以为随机数决定是否起飞
  while(num >0){
     boolean going=randomNum.nextBoolean();
     if(going){
         num -=2;
         RedArmyPlanez plane1=new RedArmyPlanez(speedOfPlanez,12,23,43,34);
         vectorOfPlanez.add(plane1);
         if(!noMissile){
            AirWar();
         }
//以为随机数决定是否从base2基地进行轰炸目标
         RedArmyPlanez plane2;
         int selectTarget=randomNum.nextInt(2);
         if(selectTarget==0){
            plane2=new RedArmyPlanez(speedOfPlanez,9,14,38,14);
         }
         else{
            plane2=new RedArmyPlanez(speedOfPlanez,9,14,43,34);
         }
         vectorOfPlanez.add(plane2);
//不断报告机群和导弹的状态,每秒一次
         try{
           showState();
           if(!noMissile){
            AirWar();
            }
           sleep(1000);
         }
         catch (InterruptedException e) {
          System.out.println(e);
        }      }
      else {
        try {
          sleep(1000);
        }
        catch (InterruptedException e) {
          System.out.println(e);
        }
      }
    }
   
//判断机群是否已经返回基地,若没有,不断报告其状态
    while (!isBack()) {
      try {
        if (!noMissile) {
          AirWar();
        }
        showState();
        sleep(1000);
      }
      catch (InterruptedException e) {
        System.out.println(e);
      }
    }
  }

解决方案 »

  1.   

    //判返回函数  
       private boolean isBack() {
        for (int i = 0; i < vectorOfPlanez.size(); i++) {
          RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
          if (Math.abs(plane.getxPositionOfPlanez() - plane.getstartYPositionOfPlanez()) <= 1) {
            plane.setifPlanezBacked(true);
            vectorOfPlanez.setElementAt(plane, i);
          }
        } 
        for (int i = 0; i < vectorOfPlanez.size(); i++) {
          RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
          if (!plane.ifPlanezBacked()) {
            return false;
          }
        }
        return true;
      }
    //显示机群状态并将其写入文件中
      private void showState() {
        for (int i = 0; i < vectorOfPlanez.size(); i++) {
          RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
          if (plane.ifPlanezBacked()) {
            //若机群已返回,则不再报告其状态
            continue;
          }
          String str1 = "机群 " ;
          String str2 = new SimpleDateFormat("mm:ss:SSS").format(plane.
              gettakeoffTimeOfPlanez());
          String str3 = new SimpleDateFormat("mm:ss:SSS").format(new
              Date());
          String str4X = String.valueOf(plane.getxPositionOfPlanez());
          String str4Y = String.valueOf(plane.getyPositionOfPlanez());
          String str5 = "飞行";
          System.out.println(str1 + i + "  " + str2 + "  " + str3 + "  " + "(" + str4X + "," 
                             + str4Y + ")" + "  " + str5);      //rr.writeFile(str1 + i + "  " + str2 + "  " + str3 + "  " + "("+str4X + "," 
                      + str4Y + ")" + "  " + str5);
    wF(str1 + i + "  " + str2 + "  " + str3 + "  " + "("+str4X + "," 
                      + str4Y + ")" + "  " + str5);    }
        System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
       // rr.writeFile("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
    wF("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
      }
    //有导弹拦截时进行空战模拟  
    private void AirWar() {
        RedArmyPlanez isFounded = null;//
        BlueArmyMissile mis = new BlueArmyMissile(speedOfMissile);
        int serial = -1;
        //导弹定位可以拦截的飞机
        for (int i = 0; i < vectorOfPlanez.size(); i++) {
          RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
          if (mis.locatePlanez(plane)) {
            if (! plane.ifPlanezBacked()){
              isFounded = plane;
            }
            serial = i;
            break;
          }
        }
        if(isFounded!=null&&numberOfMissile >0){
        //找到后进行拦截,以伪随机数决定是否拦截成功
    if (Math.abs(isFounded.getxPositionOfPlanez() - mis.getxPositionOfMissile())<distanceBetweenMissileAndPlanez) {
            numberOfMissile--;
            boolean succeed = randomNum.nextBoolean();
            if (succeed) {
              //干扰成功
              String str1 = "机群 ";
              String str2 = new SimpleDateFormat("HH:mm:ss:SSS").format(
                  isFounded.gettakeoffTimeOfPlanez());
              
              String str3 = new SimpleDateFormat("HH:mm:ss:SSS").format(new Date());
              String str4X = String.valueOf(isFounded.getxPositionOfPlanez());
              String str4Y = String.valueOf(isFounded.getxPositionOfPlanez());          String str5 = "干扰成功";
             // rr.writeFile(str1 + "  " + str2 + "  " + str3 + "  " + "("+
                           str4X +"," + str4Y +")"+ "  " + str5);
    wF(str1 + "  " + str2 + "  " + str3 + "  " + "("+
                           str4X +"," + str4Y +")"+ "  " + str5);
              System.out.println("机群干扰成功");
              System.out.println(str1 + "  " + str2 + "  " + str3 + "  " + "(" 
                                 + str4X + "," + str4Y + ")" + "  " + str5);
           }
           else {
    //拦截成功
              isFounded.setifPlanezBacked(true);
              String str1 = "导 弹";
              String str2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(
                  mis.gettimeOfEmission());
              String str3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new
                  Date());
              String str4X = String.valueOf(mis.getxPositionOfMissile());
              String str4Y = String.valueOf(mis.getxPositionOfMissile());
              String str5 = "拦截成功";
              //rr.writeFile(str1 + "  " + str2 + "  " + str3 + "  " + "(" +str4X + 
                          "," + str4Y + ")" + "  " + str5 + "  " + numberOfMissile);
    wF(str1 + "  " + str2 + "  " + str3 + "  " + "(" +str4X + 
                          "," + str4Y + ")" + "  " + str5 + "  " + numberOfMissile);
              System.out.println("导弹拦截成功");
              System.out.println(str1 + " " + str2 + " " + str3 + " " + "(" + str4X 
                                 + "," + str4Y + ")" +" " + str5 + " "+ numberOfMissile);
            }
          }
        }
      }//main函数,入口函数定义         
    public static void main(String[] arguments){
      AirWarSimulation aws=new AirWarSimulation();//AirWarSimulation类实例
      int numOfPara=arguments.length;//命令行参数的长度
    //导入命令行参数
      if (numOfPara == 1) {
          System.out.println("仿真无导弹拦截:");
          aws.noMissile = true;
          aws.seed = Long.parseLong(arguments[0]);
        }
        if (numOfPara == 4) {
          System.out.println("仿真有导弹拦截:");
          aws.noMissile = false;
          aws.seed = Long.parseLong(arguments[0]);
          aws.speedOfMissile = Integer.parseInt(arguments[1]);//导弹速度
          aws.distanceBetweenMissileAndPlanez = Integer.parseInt(arguments[2]);//导弹与飞机之间的距离
          aws.numberOfMissile = Integer.parseInt(arguments[3]);//蓝军导弹持有量
        }
    //用伪随机数产生时间,速度,机群规模
      aws.randomNum.setSeed(aws.seed);
        int number;
        do {
          number = aws.randomNum.nextInt(9); //产生0-8之间的随机数(机群规模)
        }
        while (number % 2 != 0 || number == 0); //保证是偶数且不等于0
        aws.numOfPlanez=number;
        do {
          number = aws.randomNum.nextInt(4);
        }
        while (number == 0); //产生1-3之间的随机数(机群速度)
        aws.speedOfPlanez=number;
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("机群规模  " + aws.numOfPlanez);
        System.out.println("机群速度  " + aws.speedOfPlanez);
        if (!aws.noMissile) {
          System.out.println("导弹速度 " + aws.speedOfMissile);
          System.out.println("导弹与飞机距离 D=" + aws.distanceBetweenMissileAndPlanez);
          System.out.println("基地导弹持有量 " + aws.numberOfMissile);
        }
        System.out.println("---------------------------------------------------------------");
        System.out.println(" 对象   起飞时间  当前时间  方位(X,Y)  行为标志  基地导弹保有量");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //aws.base1 = new RedArmyBase(12,39,43,34);//红军基地1
        //aws.base2 = new RedArmyBase(9,14,38,14,43,34);//红军基地2
        aws.start();//运行
      }
     }
      
    长了点。可是里面用到ResultRecorder类的地方不多。
      

  2.   

    从代码上看很复杂,可是问题又很简单。
    不是你的代码?
    你是其他语言高手,把代码翻译成java的?
    为什么要合并,分开才对呀。
      

  3.   

    如果仅仅为了在一个文件中写,可以使用内部类,改动最小。
    把public class ResultRecorder{
    这一句改为class ResultRecorder{
    将第一文件全部续写到主类文件后面。
    当然,将import java.io.*;写到主类文件最前面,不就行了。
      

  4.   

    多说无益,还是改了再说,OK??
    import java.util.*;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;public class AirWarSimulation extends Thread
    {    private long seed;//伪随机种子
        private int numOfPlanez;//红军飞机数量
        private int speedOfPlanez;//红军飞机速度
        private int speedOfMissile;//导弹速度
        private int numberOfMissile;//导弹数量
        private int distanceBetweenMissileAndPlanez;//导弹距离红军飞机之间的距离
        private boolean noMissile;//控制两种仿真情况
        private Random randomNum;//随机数
        //private RedArmyBase base1,base2;//红军基地
        Vector vectorOfPlanez;//存储机群的向量
        
        
        File resultOfRun = new File("result.txt");
        PrintWriter writer;    
        
        public synchronized void writeFile(String str)
        {
            try
            {
                writer = new PrintWriter(new FileOutputStream(resultOfRun, true),
                        true);
                writer.println(str);
                writer.close();
            } catch (IOException e)
            {
                System.out.println(e);
            }
        }
        
        public void initRecorder()
        {
            try
            {
                if (!(resultOfRun.exists()))
                {
                    resultOfRun.createNewFile();
                    writeFile("运行结果为:");
                    writeFile("-------------------------");
                    // writeFile("对象 起飞时间 当前时间 坐标 行为标志 导弹数量");
                    writeFile("-------------------------");
                }
            } catch (IOException e)
            {
                System.out.println(e);
            } 
        }
        //构造函数
        public AirWarSimulation()
        {
            randomNum = new Random();
            initRecorder();
        }    public synchronized void wF(String str)
        {
            try
            {
                PrintWriter  writer = new PrintWriter(new FileOutputStream(resultOfRun, true),
                        true);
                writer.println(str);
                writer.close();
            } catch (IOException e)
            {
                System.out.println(e);
            }
        }
      

  5.   


        //run函数
        public void run()
        {
            vectorOfPlanez = new Vector();
            int num = numOfPlanez;
            //当机群数量不为0时开始模拟飞行,以为随机数决定是否起飞
            while (num > 0)
            {
                boolean going = randomNum.nextBoolean();
                if (going)
                {
                    num -= 2;
                    RedArmyPlanez plane1 = new RedArmyPlanez(speedOfPlanez, 12, 23,
                            43, 34);
                    vectorOfPlanez.add(plane1);
                    if (!noMissile)
                    {
                        AirWar();
                    }
                    //以为随机数决定是否从base2基地进行轰炸目标
                    RedArmyPlanez plane2;
                    int selectTarget = randomNum.nextInt(2);
                    if (selectTarget == 0)
                    {
                        plane2 = new RedArmyPlanez(speedOfPlanez, 9, 14, 38, 14);
                    } else
                    {
                        plane2 = new RedArmyPlanez(speedOfPlanez, 9, 14, 43, 34);
                    }
                    vectorOfPlanez.add(plane2);
                    //不断报告机群和导弹的状态,每秒一次
                    try
                    {
                        showState();
                        if (!noMissile)
                        {
                            AirWar();
                        }
                        sleep(1000);
                    } catch (InterruptedException e)
                    {
                        System.out.println(e);
                    }
                } else
                {
                    try
                    {
                        sleep(1000);
                    } catch (InterruptedException e)
                    {
                        System.out.println(e);
                    }
                }
            }
            //判断机群是否已经返回基地,若没有,不断报告其状态
            while (!isBack())
            {
                try
                {
                    if (!noMissile)
                    {
                        AirWar();
                    }
                    showState();
                    sleep(1000);
                } catch (InterruptedException e)
                {
                    System.out.println(e);
                }
            }
        }    //判返回函数
        private boolean isBack()
        {
            for (int i = 0; i < vectorOfPlanez.size(); i++)
            {
                RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
                if (Math.abs(plane.getxPositionOfPlanez()
                        - plane.getstartYPositionOfPlanez()) <= 1)
                {
                    plane.setifPlanezBacked(true);
                    vectorOfPlanez.setElementAt(plane, i);
                }
            }
            for (int i = 0; i < vectorOfPlanez.size(); i++)
            {
                RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
                if (!plane.ifPlanezBacked())
                {
                    return false;
                }
            }
            return true;
        }    //显示机群状态并将其写入文件中
        private void showState()
        {
            for (int i = 0; i < vectorOfPlanez.size(); i++)
            {
                RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
                if (plane.ifPlanezBacked())
                {
                    //若机群已返回,则不再报告其状态
                    continue;
                }
                String str1 = "机群 ";
                String str2 = new SimpleDateFormat("mm:ss:SSS").format(plane
                        .gettakeoffTimeOfPlanez());
                String str3 = new SimpleDateFormat("mm:ss:SSS").format(new Date());
                String str4X = String.valueOf(plane.getxPositionOfPlanez());
                String str4Y = String.valueOf(plane.getyPositionOfPlanez());
                String str5 = "飞行";
                System.out.println(str1 + i + "  " + str2 + "  " + str3 + "  "
                        + "(" + str4X + "," + str4Y + ")" + "  " + str5);
                writeFile(str1 + i + " " + str2 + " " + str3 + " " + "("+str4X + "," + str4Y + ")" + " " + str5);
                wF(str1 + i + "  " + str2 + "  " + str3 + "  " + "(" + str4X + ","
                        + str4Y + ")" + "  " + str5);
            }
            System.out
                    .println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
             writeFile("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
            wF("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
        }
      

  6.   


        //有导弹拦截时进行空战模拟
        private void AirWar()
        {
            RedArmyPlanez isFounded = null;//
            BlueArmyMissile mis = new BlueArmyMissile(speedOfMissile);
            int serial = -1;
            //导弹定位可以拦截的飞机
            for (int i = 0; i < vectorOfPlanez.size(); i++)
            {
                RedArmyPlanez plane = (RedArmyPlanez) vectorOfPlanez.elementAt(i);
                if (mis.locatePlanez(plane))
                {
                    if (!plane.ifPlanezBacked())
                    {
                        isFounded = plane;
                    }
                    serial = i;
                    break;
                }
            }
            if (isFounded != null && numberOfMissile > 0)
            {
                //找到后进行拦截,以伪随机数决定是否拦截成功
                if (Math.abs(isFounded.getxPositionOfPlanez()
                        - mis.getxPositionOfMissile()) < distanceBetweenMissileAndPlanez)
                {
                    numberOfMissile--;
                    boolean succeed = randomNum.nextBoolean();
                    if (succeed)
                    {
                        String str1 = "机群 ";
                        String str2 = new SimpleDateFormat("HH:mm:ss:SSS")
                                .format(isFounded.gettakeoffTimeOfPlanez());
                        String str3 = new SimpleDateFormat("HH:mm:ss:SSS")
                                .format(new Date());
                        String str4X = String.valueOf(isFounded
                                .getxPositionOfPlanez());
                        String str4Y = String.valueOf(isFounded
                                .getxPositionOfPlanez());
                        String str5 = "干扰成功";
                        writeFile(str1 + " " + str2 + " " + str3 + " " + "("+ str4X +"," + str4Y +")"+ " " + str5);
                        wF(str1 + "  " + str2 + "  " + str3 + "  " + "(" + str4X
                                + "," + str4Y + ")" + "  " + str5);
                        System.out.println("机群干扰成功");
                        System.out.println(str1 + "  " + str2 + "  " + str3 + "  "
                                + "(" + str4X + "," + str4Y + ")" + "  " + str5);
                    } else
                    {
                        //拦截成功
                        isFounded.setifPlanezBacked(true);
                        String str1 = "导 弹";
                        String str2 = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss:SSS").format(mis
                                .gettimeOfEmission());
                        String str3 = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss:SSS").format(new Date());
                        String str4X = String.valueOf(mis.getxPositionOfMissile());
                        String str4Y = String.valueOf(mis.getxPositionOfMissile());
                        String str5 = "拦截成功";
                        writeFile(str1 + " " + str2 + " " + str3 + " " + "("
                         +str4X + "," + str4Y + ")" + " " + str5 + " " +
                         numberOfMissile);
                        wF(str1 + "  " + str2 + "  " + str3 + "  " + "(" + str4X
                                + "," + str4Y + ")" + "  " + str5 + "  "
                                + numberOfMissile);
                        System.out.println("导弹拦截成功");
                        System.out.println(str1 + " " + str2 + " " + str3 + " "
                                + "(" + str4X + "," + str4Y + ")" + " " + str5
                                + " " + numberOfMissile);
                    }
                }
            }
        }    //main函数,入口函数定义
        public static void main(String[] arguments)
        {
            AirWarSimulation aws = new AirWarSimulation();//AirWarSimulation类实例
            int numOfPara = arguments.length;//命令行参数的长度
            //导入命令行参数
            if (numOfPara == 1)
            {
                System.out.println("仿真无导弹拦截:");
                aws.noMissile = true;
                aws.seed = Long.parseLong(arguments[0]);
            }
            if (numOfPara == 4)
            {
                System.out.println("仿真有导弹拦截:");
                aws.noMissile = false;
                aws.seed = Long.parseLong(arguments[0]);
                aws.speedOfMissile = Integer.parseInt(arguments[1]);//导弹速度
                aws.distanceBetweenMissileAndPlanez = Integer
                        .parseInt(arguments[2]);//导弹与飞机之间的距离
                aws.numberOfMissile = Integer.parseInt(arguments[3]);//蓝军导弹持有量
            }
            //用伪随机数产生时间,速度,机群规模
            aws.randomNum.setSeed(aws.seed);
            int number;
            do
            {
                number = aws.randomNum.nextInt(9); //产生0-8之间的随机数(机群规模)
            } while (number % 2 != 0 || number == 0); //保证是偶数且不等于0
            aws.numOfPlanez = number;
            do
            {
                number = aws.randomNum.nextInt(4);
            } while (number == 0); //产生1-3之间的随机数(机群速度)
            aws.speedOfPlanez = number;
            System.out
                    .println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            System.out.println("机群规模  " + aws.numOfPlanez);
            System.out.println("机群速度  " + aws.speedOfPlanez);
            if (!aws.noMissile)
            {
                System.out.println("导弹速度 " + aws.speedOfMissile);
                System.out.println("导弹与飞机距离 D="
                        + aws.distanceBetweenMissileAndPlanez);
                System.out.println("基地导弹持有量 " + aws.numberOfMissile);
            }
            System.out
                    .println("---------------------------------------------------------------");
            System.out.println(" 对象   起飞时间  当前时间  方位(X,Y)  行为标志  基地导弹保有量");
            System.out
                    .println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            //aws.base1 = new RedArmyBase(12,39,43,34);//红军基地1
            //aws.base2 = new RedArmyBase(9,14,38,14,43,34);//红军基地2
            aws.start();//运行
        }
    }
      

  7.   

    我在ECLIPSE下把你的两个类导入的,
    但是我发现,还有两个类,你没有给,
    我顺手也加上了两个空类,
    如果你要的话,
    我可以把工程式MAIL给你