JAvaキーボードから10個の数を入力し、結果を計算する

1402 ワード

import java.io.*;

public class SortCount {

static InputStreamReader in;
static BufferedReader reader;
static {
	in = new InputStreamReader(System.in);
	reader = new BufferedReader(in);
}

//     
public static int readInt() {
  int i = 0;
  try {
    String s1 = reader.readLine();
    i = Integer.parseInt(s1);
  } catch (Exception e) {
	System.exit( 0 );
  }
  return i;
}

//     
public static void sort(int[] num) {
	int mid;
	int i, j;
	for (i = 0; i < 10; i++)
		for (j = i + 1; j < 10; j++)
			if (num[i] > num[j]) {
				mid = num[i];
				num[i] = num[j];
				num[j] = mid;
			}
}

//     
public static int total(int[] num) {		
	int result = 0;
	for (int i = 0; i < num.length; i++) {
		if (num[i]>0)
			result = result + num[i];
	}
	return result;
}

public static void main(String args[]) {
	int a[] = new int[10];
	for (int i = 0; i < 10; i++) {
		System.out.println("    " + (i + 1) + "  :");
		a[i] = SortCount.readInt();
	}
	
	//   
	sort(a);
	
	//   
	int result = total(a);
	System.out.println(result);
}
}