如何在阵列的相应位置打印这些数字?

时间:2017-08-08 20:33:45

标签: java arrays file java.util.scanner

import java.util.*;
import java.io.*;
public class PracticeFinal {
   public static void main (String[] args) throws FileNotFoundException{
       Scanner input = new Scanner (new File("altitude.txt"));
       int[] x = new int [400];
       int[] y = new int [400];
       float[] altitude = new float [400];
       x[0] = 0;
       y[0] = 0;
       altitude[0] = 0;
       int i = 0;
       System.out.println("x\ty\taltitude");
      while (input.hasNextFloat()) {
          int line = input.nextInt();
          x[i]=line;
          int line2=input.nextInt();
          y[i]=line2;
          float line3 = input.nextFloat();
          altitude[i]=line3;
          System.out.println(x[i]+"\t" + y[i]+"\t"+ altitude[i]+"\t");
          i+=1; }
           printMax(altitude,x,y);
           printMin(altitude,x,y);
           printAverage(altitude);
           printContour(altitude,x,y);
        }   
   public static void printMax(float []altitude, int []x, int []y) {
       float max=altitude[0];
       int ind=0;
       for(int i=0;i<100;i++)
       {
           if (altitude[i]>max)
           {
               max = altitude[i];
               ind=i;
            }
        }
        System.out.println("The maximum altitude is "+"("+x[ind]+","+y[ind]+","+max+")");
    }
    public static void printMin(float []altitude, int []x, int []y){
        float min=altitude[0];
        int ind=0;
        for(int i=0;i<100;i++)
        {
            if (altitude[i] < min)
            {
                min = altitude[i];
                ind=i;
            }
        }
        System.out.println("The minimum altitude is "+"("+x[ind]+","+y[ind]+","+min+")");
   }
   public static void printAverage(float []altitude){
       float total = 0;
       for(int i=0;i<100;i++) 
       {
           total += altitude[i];
        }
        float avg = total / 100;
        System.out.printf("The aveage altitude of the region is " + "%.2f", avg);
        System.out.println();
   }
   public static void printContour(float []altitude, int[]x, int[]y){
       for (int i=0;i<100;i++) {
       System.out.print (altitude[i] + " ");    


}
}
}

我需要帮助这个程序的最后一部分有一个x和y图表,高度为ex,0,0 = 109.1 1,0 = 200 所以对于最后一种方法,我需要打印出相应位置的高度,从1,1到2,1到3,1等等 在for循环之后不知道下一步该做什么 这是文件im读取第一个数字是x秒是y,第三个是海拔。

1   1   101.0 
2   2   97.2 
3   3   112.3 
4   4   114.2 
5   5   100.2 
6   6   97.5 
7   7   97.8 
8   8   81.3 
9   9   105.4 
10  10  108.7 
3   1   107.8 
3   2   115.4 
3   4   118.3 
3   5   120.3 
3   6   122.3 
3   7   90.3 
3   8   81.7 
3   9   87.4 
3   10  113.2 
1   2   102.3 
1   3   104.5 
1   4   109.8 
1   5   99.8 
1   6   88.9 
1   7   89.3 
1   8   100.1 
1   9   110.8 
1   10  98.3 
2   1   98.8 
2   3   80.5 
2   4   85.1 
2   5   83.2 
2   6   92.3 
2   7   94.3 
2   8   199.3 
2   9   104.3 
2   10  105.2 
4   1   120.5 
4   2   87.3 
4   3   82.3 
5   1   83.2 
5   2   84.5 
5   3   96.9 
5   4   86.7 
4   5   115.3 
4   6   101.3 
4   7   102.6 
4   8   106.9 
4   9   109.8 
4   10  93.4 
6   7   93.2 
6   8   92.4 
6   9   80.9 
6   10  81.2 
5   6   102.3 
5   7   101.1 
5   8   106.7 
5   9   105.3 
5   10  88.9 
6   1   80.8 
6   2   81.3 
6   3   84.5 
6   4   90.8 
6   5   99.8 
8   1   98.6 
8   2   101.1 
8   3   103.5 
8   4   104.6 
8   5   105.8 
8   6   80.9 
8   7   80.1 
7   1   98.3 
7   2   96.4 
7   3   95.1 
7   4   83.4 
7   5   95.1 
7   6   96.3 
7   8   99.9 
7   9   100.0 
7   10  102.3 
10  3   99.9 
10  4   98.3 
10  5   91.3 
10  6   94.3 
10  7   95.3 
10  8   103.2 
8   9   90.2 
8   10  91.3 
9   1   93.2 
9   2   81.5 
9   3   91.0 
9   4   95.3 
9   5   122.3 
9   6   119.8 
9   7   118.7 
9   8   117.3 
9   10  107.8 
10  1   108.8 
10  2   109.9 
10  9   104.2 

1 个答案:

答案 0 :(得分:0)

public static void printContour(float []altitude, int[]x, int[]y){
   for (int i = 0; i < 100; i++) {
        for (int a = 0; a < 100; a++) {
            if (x[i] <  x[a]) {
                int temp_for_x = x[i];
                x[i] = x[a];
                x[a] = temp_for_x;
                int temp_for_y = y[i];
                y[i] = y[a];
                y[a] = temp_for_y;
                float temp_for_alt = altitude[i];
                altitude[i] = altitude[a];
                altitude[a] = temp_for_alt;
            }
            if(y[i] < y[a]){
                int temp_for_x = x[i];
                x[i] = x[a];
                x[a] = temp_for_x;
                int temp_for_y = y[i];
                y[i] = y[a];
                y[a] = temp_for_y;
                float temp_for_alt = altitude[i];
                altitude[i] = altitude[a];
                altitude[a] = temp_for_alt;
            }
        }
    }

    for (int incr=0;incr<100;incr++) {
         System.out.print(x[incr]+ "   " + y[incr] + "   " + altitude[incr]+ "   ");
         if(incr%10 == 9)
            System.out.println();

   }



}
}

您可以使用更好的排序算法Here