/*
*题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
**/
package com.cn.testquestion;
import java.io.*;
public class StudentGrate
{
public static void main(String args[])throws Exception
{
Student[] student={
new Student("10001","LiNing",88,99,68),
new Student("10002","LiLie",80,79,85),
new Student("10003","YunFei",88,90,67),
new Student("10004","ChunLi",55,91,73),
new Student("10005","ZhangSan",96,80,75)
};
/*
Student student1=new Student("10001","LiNing",88,99,68);
Student student2=new Student("10002","LiLie",80,79,85);
Student student3=new Student("10003","YunFei",88,90,67);
Student student4=new Student("10004","ChunLi",55,91,73);
Student student5=new Student("10005","ZhangSan",96,80,75);
*/
for(int i=0;i<student.length;i++)
{
new FileStudent(student[i]).toFileIn();
}
}
}
class Student
{
private String xuehao;
private String name;
private int shuxue;
private int yuwen;
private int yingyu;
private int average;
public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
{
this.xuehao=xuehao;
this.name=name;
this.shuxue=shuxue;
this.yuwen=yuwen;
this.yingyu=yingyu;
this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
}
public String toString()
{
return "学号:"+this.xuehao+" 姓名:"+this.name+" 数学成绩:"+this.shuxue+" 语文成绩:"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+" === ";
}
}
class FileStudent
{
private static String str="";
private Student person;
public FileStudent(Object person)
{
this.person=(Student)person;
}
public void toFileIn()throws Exception
{
//String str="";
File file=new File("stud.txt");
if(!file.exists())
file.createNewFile();
//else
// System.out.println("文件已经存在");
FileWriter fileto=new FileWriter(file);
str=str+person.toString();
fileto.write(str);
fileto.close();
}
}
这个是之前看到的一个题目自己试了下,能够打印到文件,但是不能换行。用过 /n 但没成功,不知道有没有别的办法实现。
*题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
**/
package com.cn.testquestion;
import java.io.*;
public class StudentGrate
{
public static void main(String args[])throws Exception
{
Student[] student={
new Student("10001","LiNing",88,99,68),
new Student("10002","LiLie",80,79,85),
new Student("10003","YunFei",88,90,67),
new Student("10004","ChunLi",55,91,73),
new Student("10005","ZhangSan",96,80,75)
};
/*
Student student1=new Student("10001","LiNing",88,99,68);
Student student2=new Student("10002","LiLie",80,79,85);
Student student3=new Student("10003","YunFei",88,90,67);
Student student4=new Student("10004","ChunLi",55,91,73);
Student student5=new Student("10005","ZhangSan",96,80,75);
*/
for(int i=0;i<student.length;i++)
{
new FileStudent(student[i]).toFileIn();
}
}
}
class Student
{
private String xuehao;
private String name;
private int shuxue;
private int yuwen;
private int yingyu;
private int average;
public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
{
this.xuehao=xuehao;
this.name=name;
this.shuxue=shuxue;
this.yuwen=yuwen;
this.yingyu=yingyu;
this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
}
public String toString()
{
return "学号:"+this.xuehao+" 姓名:"+this.name+" 数学成绩:"+this.shuxue+" 语文成绩:"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+" === ";
}
}
class FileStudent
{
private static String str="";
private Student person;
public FileStudent(Object person)
{
this.person=(Student)person;
}
public void toFileIn()throws Exception
{
//String str="";
File file=new File("stud.txt");
if(!file.exists())
file.createNewFile();
//else
// System.out.println("文件已经存在");
FileWriter fileto=new FileWriter(file);
str=str+person.toString();
fileto.write(str);
fileto.close();
}
}
这个是之前看到的一个题目自己试了下,能够打印到文件,但是不能换行。用过 /n 但没成功,不知道有没有别的办法实现。
解决方案 »
- 编译原理的语法分析
- 物理隔绝的两个网络之间进行数据交换,在线程这里遇到问题
- 紧急求助:WebSphere MQ的事务管理问题,解决马上结贴
- 登陆后如何处理登陆窗口
- IO问题!
- 如何确定程序的安装路径
- 如何在applet的窗体或组件上显示图片
- 首次在java 上帖子!!以后请大家多多关照,谢了!!!
- 为什么我定义了一个JMenuItem对象后,同时用JPopMenu和JMenu调用,但只能显示一个?
- java -classpath *1.jar;*2.jar 其中*1.jar与*2.jar之间是分号还是冒号,还是其他符号,我的系统是win2000
- 关于 JTextField 的 setColumns()
- java 环境变量配置问题
package test;import java.io.*;
public class Test
{
public static void main(String args[])throws Exception
{
Student[] student={
new Student("10001","LiNing",88,99,68),
new Student("10002","LiLie",80,79,85),
new Student("10003","YunFei",88,90,67),
new Student("10004","ChunLi",55,91,73),
new Student("10005","ZhangSan",96,80,75)
};
/*
Student student1=new Student("10001","LiNing",88,99,68);
Student student2=new Student("10002","LiLie",80,79,85);
Student student3=new Student("10003","YunFei",88,90,67);
Student student4=new Student("10004","ChunLi",55,91,73);
Student student5=new Student("10005","ZhangSan",96,80,75);
*/
for(int i=0;i<student.length;i++)
{
new FileStudent(student[i]).toFileIn();
}
}
}
class Student
{
private String xuehao;
private String name;
private int shuxue;
private int yuwen;
private int yingyu;
private int average;
public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
{
this.xuehao=xuehao;
this.name=name;
this.shuxue=shuxue;
this.yuwen=yuwen;
this.yingyu=yingyu;
this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
}
public String toString()
{
return "学号:\n"+this.xuehao+" 姓名:\n"+this.name+" 数学成绩:\n"+this.shuxue+" 语文成绩:\n"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+" === ";
}
}
class FileStudent
{
private static String str="";
private Student person;
public FileStudent(Object person)
{
this.person=(Student)person;
}
public void toFileIn()throws Exception
{
//String str="";
File file=new File("stud.txt");
if(!file.exists())
file.createNewFile();
//else
// System.out.println("文件已经存在");
FileWriter fileto=new FileWriter(file);
str=str+person.toString();
fileto.write(str);
fileto.close();
}
}
这个是文件内容
学号:
10001 姓名:
LiNing 数学成绩:
88 语文成绩:
99 英语成绩:68 平均成绩:85 === 学号:
10002 姓名:
LiLie 数学成绩:
80 语文成绩:
79 英语成绩:85 平均成绩:81 === 学号:
10003 姓名:
YunFei 数学成绩:
88 语文成绩:
90 英语成绩:67 平均成绩:81 === 学号:
10004 姓名:
ChunLi 数学成绩:
55 语文成绩:
91 英语成绩:73 平均成绩:73 === 学号:
10005 姓名:
ZhangSan 数学成绩:
96 语文成绩:
80 英语成绩:75 平均成绩:83 === 而且,貌似换行只有\n吧
你说的那个我真的试了,真的不行
*题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
**/
package com.cn.testquestion;
import java.io.*;
public class StudentGrate
{
public static void main(String args[])throws Exception
{
Student[] student={
new Student("10001","LiNing",88,99,68),
new Student("10002","LiLie",80,79,85),
new Student("10003","YunFei",88,90,67),
new Student("10004","ChunLi",55,91,73),
new Student("10005","ZhangSan",96,80,75)
};
/*
Student student1=new Student("10001","LiNing",88,99,68);
Student student2=new Student("10002","LiLie",80,79,85);
Student student3=new Student("10003","YunFei",88,90,67);
Student student4=new Student("10004","ChunLi",55,91,73);
Student student5=new Student("10005","ZhangSan",96,80,75);
*/
for(int i=0;i<student.length;i++)
{
new FileStudent(student[i]).toFileIn();
}
}
}
class Student
{
private String xuehao;
private String name;
private int shuxue;
private int yuwen;
private int yingyu;
private int average;
public Student(String xuehao,String name,int shuxue,int yuwen,int yingyu)
{
this.xuehao=xuehao;
this.name=name;
this.shuxue=shuxue;
this.yuwen=yuwen;
this.yingyu=yingyu;
this.average=(this.shuxue+this.yuwen+this.yingyu)/3;
}
public String toString()
{
String nextline=System.getProperty("line.separator"); return "学号:"+this.xuehao+" 姓名:"+this.name+" 数学成绩:"+this.shuxue+" 语文成绩:"+this.yuwen+" 英语成绩:"+this.yingyu+" 平均成绩:"+this.average+nextline;
}
}
class FileStudent
{
private static String str="";
private Student person;
public FileStudent(Object person)
{
this.person=(Student)person;
}
public void toFileIn()throws Exception
{
//String str="";
File file=new File("stud.txt");
if(!file.exists())
file.createNewFile();
//else
// System.out.println("文件已经存在");
FileWriter fileto=new FileWriter(file);
str=str+person.toString();
fileto.write(str);
fileto.close();
}
}
学到了,谢谢大家。用的是txt文档打开的,能够实现换行了。