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();
}
}
2014-05-28
展开全部
System.out.println("输出内容")
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询