java 控制台的输出 简单的一个函数

如题所述

public static void main(String args[]) {
File file = new File("D:\\test.txt");
Scanner sc = new Scanner(System.in);
input(sc, file);
double[] scores = output(sc, file);
for(int i=scores.length-1; i>=0; i--){
System.out.print(scores[i] + " ");
}
System.out.println("平均成绩: " + avg(scores));

}

public static double avg(double[] scores){
double sum = 0;
for(int i=0; i<scores.length; i++){
sum += scores[i];
}
return sum/scores.length;
}
public static double[] output(Scanner sc, File file){
double[] scores = new double[3];
try {
sc = new Scanner(file);
int i=0;
while(sc.hasNextDouble()){
scores[i++] = sc.nextDouble();
if(i==3){
break;
}
}
Arrays.sort(scores);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return scores;
}

public static void input(Scanner sc, File file){
String text = "";
for(int i=0; i<3; i++){
System.out.println("请输入成绩 0 - 100");
double score = sc.nextDouble();
if(i == 2){
text += score;
break;
}
text += score +" ";
}
try {
FileWriter fw = new FileWriter(file);       //创建文件写入  
            BufferedWriter bw = new BufferedWriter(fw); 
bw.write(text);
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-05-28
System.out.println("输出内容")
相似回答