import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] args)throws Exception {
PrintStream ps=null;
try{
FileOutputStream i=new FileOutputStream("E:/avg.txt");
ps=new PrintStream(i);
}catch(IOException e){
e.printStackTrace();
}
if(ps!=null){
System.setOut(ps);
}
int sum=0;
int avg=0;
InputStreamReader s=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(s);
String [] d=new String[5];
int [] e=new int[5];
System.out.println("输入5个同学的学号:");
for(int x=0;x<d.length;x++){
d[x]=br.readLine();
e[x]=Integer.parseInt(d[x]);
}
String [] c=new String[5];
System.out.println("输入5个同学的姓名:");
for(int j=0;j<c.length;j++){
c[j]=br.readLine();
}
String [] b=new String[5];
int[] a=new int[5];
System.out.println("输入5个同学的分数:");
for(int i=0;i<5;i++){
b[i]=br.readLine();
a[i]=Integer.parseInt(b[i]);
sum+=a[i];
avg=sum/5;
}
System.out.println("5个同学的平均成绩为:"+avg);
}
}
为什么我的在AVG.txt只能输入5个同学的姓名而其它的学号和分数还有结果不能出来
请帮我讲一下
import java.util.*;
public class Test{
public static void main(String[] args)throws Exception {
PrintStream ps=null;
try{
FileOutputStream i=new FileOutputStream("E:/avg.txt");
ps=new PrintStream(i);
}catch(IOException e){
e.printStackTrace();
}
if(ps!=null){
System.setOut(ps);
}
int sum=0;
int avg=0;
InputStreamReader s=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(s);
String [] d=new String[5];
int [] e=new int[5];
System.out.println("输入5个同学的学号:");
for(int x=0;x<d.length;x++){
d[x]=br.readLine();
e[x]=Integer.parseInt(d[x]);
}
String [] c=new String[5];
System.out.println("输入5个同学的姓名:");
for(int j=0;j<c.length;j++){
c[j]=br.readLine();
}
String [] b=new String[5];
int[] a=new int[5];
System.out.println("输入5个同学的分数:");
for(int i=0;i<5;i++){
b[i]=br.readLine();
a[i]=Integer.parseInt(b[i]);
sum+=a[i];
avg=sum/5;
}
System.out.println("5个同学的平均成绩为:"+avg);
}
}
为什么我的在AVG.txt只能输入5个同学的姓名而其它的学号和分数还有结果不能出来
请帮我讲一下
而姓名和分数不能输入还有平均数也不能显示出来
高手能帮我看下吗
因为你已经重定向系统输出到文件了,所以直接用System.out.print就能输出到文件了System.out.println("输入5个同学的学号:");
for(int x=0;x<d.length;x++){
d[x]=br.readLine();
e[x]=Integer.parseInt(d[x]);
System.out.println(e[x]); //这里输出
}
String [] c=new String[5];
System.out.println("输入5个同学的姓名:");
for(int j=0;j<c.length;j++){
c[j]=br.readLine();
System.out.println(c[j]); //这里输出
}
String [] b=new String[5];
int[] a=new int[5];
System.out.println("输入5个同学的分数:");
for(int i=0;i<5;i++){
b[i]=br.readLine();
a[i]=Integer.parseInt(b[i]);
System.out.println(b[i]); //这里输出
sum+=a[i];
//avg=sum/5; //这个没必要在循环里计算
}
avg = sum/5; //放到循环外计算
System.out.println("5个同学的平均成绩为:"+avg);
正解。
其实你这样一个一个的输好像有点麻烦,可以做成批量输入:
...
String[] d = br.readLine().split(",");
int[] e = new int[5];
System.out.println("输入5个同学的学号:");
for (int x = 0; x < d.length; x++) {
e[x] = Integer.parseInt(d[x]);
System.out.println(e[x]);
}
...
因为你在控制台看不到提示符,所以你刚开始输入学号的时候输入了非数字,那就出异常终了了
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] args)throws Exception {
PrintStream ps=null;
try{
FileOutputStream i=new FileOutputStream("E:/avg.txt");
ps=new PrintStream(i);}catch(IOException e){
e.printStackTrace();
}
//if(ps!=null){
//System.setOut(ps); //这里不要,这样在控制台可以看到输出
//}
int sum=0;
int avg=0;
InputStreamReader s=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(s);
String [] d=new String[5];
int [] e=new int[5];
System.out.println("输入5个同学的学号:"); //输出到控制台
ps.println("输入5个同学的学号:"); //输出到文件
for(int x=0;x<d.length;x++){
d[x]=br.readLine();
e[x]=Integer.parseInt(d[x]); //这里如果输入了非法数字,程序就异常终了
//System.out.println(e[x]); //这里输出
ps.println(e[x]);
}
String [] c=new String[5];
System.out.println("输入5个同学的姓名:");
ps.println("输入5个同学的姓名:");
for(int j=0;j<c.length;j++){
c[j]=br.readLine();
//System.out.println(c[j]); //这里输出
ps.println(c[j]);
}
String [] b=new String[5];
int[] a=new int[5];
System.out.println("输入5个同学的分数:");
ps.println("输入5个同学的分数:");
for(int i=0;i<5;i++){
b[i]=br.readLine();
a[i]=Integer.parseInt(b[i]); //这里如果输入了非法数字,程序就异常终了
//System.out.println(b[i]); //这里输出
ps.println(b[i]);
sum+=a[i];
//avg=sum/5; //这个没必要在循环里计算
}
avg = sum/5; //放到循环外计算
System.out.println("5个同学的平均成绩为:"+avg);
ps.println("5个同学的平均成绩为:"+avg);
ps.close();
}