市町村のデータをソートし、その平均と最大、最小を求めたいのですが、
平均と最大がうまくできません。
平均は"二項演算子'+'のオペランド型が不正です"と表示され、intからTownに直せないことはわかるのですが、全くどうしたらいいのかわかりません。
最大はもはやエラーも出ないし、実行した時に表示もされません。
どうしたら解決することができるでしょうか?
よろしくお願いします。
java
1import java.util.Scanner; 2class Town { 3 String pref; 4 String name; 5 int population; 6} 7 8public class Population { 9 public static void main(String args[]) { 10 Scanner kbd = new Scanner(System.in); 11 String p=null; 12 String n=null; 13 int num=0; 14 Town[] table = new Town[2000]; 15 Town temp; 16 int count=0; 17 18 for(int i=0; kbd.hasNext() && i<2000; i++) { 19 p = kbd.next(); 20 System.out.println(p); 21 n = kbd.next(); 22 System.out.println(n); 23 num = kbd.nextInt(); 24 System.out.println(num); 25 temp = new Town(); 26 temp.pref = p; 27 temp.name = n; 28 temp.population = num; 29 table[i] = temp; 30 count++; 31 } 32 33 sort(table,count); 34 35 } 36 37 38 public static void sort(Town[] t,int num) { 39 Town temp; 40 41 for(int i=0; i<num; i++) { 42 for(int j=i+1; j<num; j++) { 43 if(t[i].population<t[j].population) { 44 temp = t[i]; 45 t[i] = t[j]; 46 t[j] = temp; 47 } 48 } 49 } 50 51 Middle(t,num); 52 53 } 54 55 public static void Middle(Town[] t, int num) { 56 57 System.out.println("県 : "+t[(num-1)/2].pref+": 市町村 :" 58 +t[(num-1)/2].name+" : 人口 : "+t[(num-1)/2].population); 59 Average(t,num); 60 } 61 62 public static void Average(Town[] t, int num) { 63//このメソッドと 64 int total=0; 65 double ave=0; 66 for(int i=0; i<t.length; i++) { 67 total += t[i]; 68 } 69 ave = (double)total/(double)(num-1); 70 System.out.println("市町村人口の平均は"+ave); 71 72 Max(t,num); 73 74 } 75 76 public static void Max(Town[] t, int num) { //ここです。 77 for(int i=0; i>t.length; i++) { 78 System.out.println("人口が最大の市町村は 県 : "+t[num-1].pref+": 市町村 :"+t[num-1].name+" : 人口 : "+t[num-1].population); 79 break; 80 } 81 82 Min(t,num); 83 84 } 85 86 public static void Min(Town[] t, int num) { 87 for(int i=0; i<1; i++) 88 System.out.println("人口が最小の市町村は 県 : "+t[num-1].pref+": 市町村 :"+t[num-1].name+" : 人口 : "+t[num-1].population); 89 } 90 91} 92 93
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/08 10:57