如何详细解析Java将学生成绩输出至磁盘文件的方法?

2026-05-26 01:281阅读0评论SEO基础
  • 内容介绍
  • 相关推荐

本文共计1445个文字,预计阅读时间需要6分钟。

如何详细解析Java将学生成绩输出至磁盘文件的方法?

目录

一、项目描述

二、解题思路

三、代码详解

四、方法二:引入Hutool

一、项目描述

项目:计算五名学生的三门课程成绩,从键盘输入以上数据(包括学生号、姓名、三门课程成绩)。

二、解题思路

1.定义学生类,包含学生号、姓名、三门课程成绩属性。

2.创建学生列表,用于存储所有学生信息。

3.循环读取输入的学生信息,创建学生对象并添加到列表中。

4.计算每个学生的平均成绩。

5.输出所有学生的信息及平均成绩。

三、代码详解

pythonclass Student: def __init__(self, student_id, name, scores): self.student_id=student_id self.name=name self.scores=scores self.average=sum(scores) / len(scores)

def input_students_info(): students=[] while True: student_id=input(请输入学生号(或输入'q'退出):) if student_id=='q': break name=input(请输入姓名:) scores=list(map(float, input(请输入三门课程成绩,用空格分隔:).split())) if len(scores) !=3: print(输入成绩数量错误,请重新输入!) continue student=Student(student_id, name, scores) students.append(student) return students

def print_students_info(students): for student in students: print(f学生号:{student.student_id}, 姓名:{student.name}, 成绩:{student.scores}, 平均成绩:{student.average:.2f})

students=input_students_info()print_students_info(students)

四、方法二:引入Hutoolpythonimport hutool.core.convert.Convert

class Student: def __init__(self, student_id, name, scores): self.student_id=student_id self.name=name self.scores=Convert.toList(float, scores) self.average=sum(self.scores) / len(self.scores)

def input_students_info(): students=[] while True: student_id=input(请输入学生号(或输入'q'退出):) if student_id=='q': break name=input(请输入姓名:) scores=input(请输入三门课程成绩,用空格分隔:).split() if len(scores) !=3: print(输入成绩数量错误,请重新输入!) continue student=Student(student_id, name, scores) students.append(student) return students

def print_students_info(students): for student in students: print(f学生号:{student.student_id}, 姓名:{student.name}, 成绩:{student.scores}, 平均成绩:{student.average:.2f})

students=input_students_info()print_students_info(students)

目录
  • 一、题目描述
  • 二、解题思路
  • 三、代码详解
    • 解法二:引入Hutool

一、题目描述

题目:有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),

把这些数据存放在磁盘文件 "stud.txt "中。

二、解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);

2、把学生和成绩拼接成字符串

3、把字符串保存到硬盘文件 "stud.txt "中。

三、代码详解

public class Basics102 { public static void fileWriter(String str) { FileWriter fw =null; try { fw = new FileWriter("D:\\stud.txt", true); //如["\\stud.txt"]则表示在项目盘符的根目录建立文件,如项目在F盘,则在F盘根目录建立文件 //如["save\\stud.txt"]则表示在当前项目文件夹里找到命名为[save]的文件夹,把文件新建在该文件夹内 System.out.println("数据已成功写入"); fw.write(str); fw.close(); } catch (Exception e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { try { //操作完要回收流 fw.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { int i,j,k=1; String id[] = new String[5]; int score[] = new int[15]; String name[] = new String[5]; String str=null; Scanner inputId = new Scanner(System.in); Scanner inputName = new Scanner(System.in); Scanner inputScore = new Scanner(System.in); for(i=0;i<5;i++) { System.out.print("请输入第"+(i+1)+"位同学的学号:"); id[i]=inputId.nextLine(); System.out.print("请输入第"+(i+1)+"位同学的姓名:"); name[i]=inputName.nextLine(); for(j=i*3;j<i*3+3;j++) { System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:"); score[j]=inputScore.nextInt(); } k=1; } for(i=0;i<5;i++) { str="学号"+id[i]; str=str+" "+"姓名"+name[i]; for(j=i*3;j<i*3+3;j++) str=str+" "+"第"+k+++"门成绩="+score[j]; k=1; System.out.println(); fileWriter(str+"\r\n"); } } }

解法二:引入Hutool

解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);

2、把学生和成绩拼接成字符串

3、把字符串保存到硬盘文件 "stud.txt "中。

在上一个解法的基础上,优化了第三步,使用

如何详细解析Java将学生成绩输出至磁盘文件的方法?

将String写入文件,UTF-8编码追加模式

FileUtil.appendUtf8String(str,"D:\stud2.txt");

代码详解

public class Basics102_2 { public static void main(String[] args) { int i,j,k=1; String id[] = new String[5]; int score[] = new int[15]; String name[] = new String[5]; String str=null; Scanner inputId = new Scanner(System.in); Scanner inputName = new Scanner(System.in); Scanner inputScore = new Scanner(System.in); for(i=0;i<5;i++) { System.out.print("请输入第"+(i+1)+"位同学的学号:"); id[i]=inputId.nextLine(); System.out.print("请输入第"+(i+1)+"位同学的姓名:"); name[i]=inputName.nextLine(); for(j=i*3;j<i*3+3;j++) { System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:"); score[j]=inputScore.nextInt(); } k=1; } for(i=0;i<5;i++) { str="学号"+id[i]; str=str+" "+"姓名"+name[i]; for(j=i*3;j<i*3+3;j++) str=str+" "+"第"+k+++"门成绩="+score[j]; str +="\n"; k=1; try { //不需要关闭文件流,源码已经有了 FileUtil.appendUtf8String(str,"D:\\stud2.txt"); }catch (IORuntimeException e){ //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); } } } }

到此这篇关于Java实现学生成绩输出到磁盘文件的方法详解的文章就介绍到这了,更多相关Java成绩输出到磁盘文件内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计1445个文字,预计阅读时间需要6分钟。

如何详细解析Java将学生成绩输出至磁盘文件的方法?

目录

一、项目描述

二、解题思路

三、代码详解

四、方法二:引入Hutool

一、项目描述

项目:计算五名学生的三门课程成绩,从键盘输入以上数据(包括学生号、姓名、三门课程成绩)。

二、解题思路

1.定义学生类,包含学生号、姓名、三门课程成绩属性。

2.创建学生列表,用于存储所有学生信息。

3.循环读取输入的学生信息,创建学生对象并添加到列表中。

4.计算每个学生的平均成绩。

5.输出所有学生的信息及平均成绩。

三、代码详解

pythonclass Student: def __init__(self, student_id, name, scores): self.student_id=student_id self.name=name self.scores=scores self.average=sum(scores) / len(scores)

def input_students_info(): students=[] while True: student_id=input(请输入学生号(或输入'q'退出):) if student_id=='q': break name=input(请输入姓名:) scores=list(map(float, input(请输入三门课程成绩,用空格分隔:).split())) if len(scores) !=3: print(输入成绩数量错误,请重新输入!) continue student=Student(student_id, name, scores) students.append(student) return students

def print_students_info(students): for student in students: print(f学生号:{student.student_id}, 姓名:{student.name}, 成绩:{student.scores}, 平均成绩:{student.average:.2f})

students=input_students_info()print_students_info(students)

四、方法二:引入Hutoolpythonimport hutool.core.convert.Convert

class Student: def __init__(self, student_id, name, scores): self.student_id=student_id self.name=name self.scores=Convert.toList(float, scores) self.average=sum(self.scores) / len(self.scores)

def input_students_info(): students=[] while True: student_id=input(请输入学生号(或输入'q'退出):) if student_id=='q': break name=input(请输入姓名:) scores=input(请输入三门课程成绩,用空格分隔:).split() if len(scores) !=3: print(输入成绩数量错误,请重新输入!) continue student=Student(student_id, name, scores) students.append(student) return students

def print_students_info(students): for student in students: print(f学生号:{student.student_id}, 姓名:{student.name}, 成绩:{student.scores}, 平均成绩:{student.average:.2f})

students=input_students_info()print_students_info(students)

目录
  • 一、题目描述
  • 二、解题思路
  • 三、代码详解
    • 解法二:引入Hutool

一、题目描述

题目:有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),

把这些数据存放在磁盘文件 "stud.txt "中。

二、解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);

2、把学生和成绩拼接成字符串

3、把字符串保存到硬盘文件 "stud.txt "中。

三、代码详解

public class Basics102 { public static void fileWriter(String str) { FileWriter fw =null; try { fw = new FileWriter("D:\\stud.txt", true); //如["\\stud.txt"]则表示在项目盘符的根目录建立文件,如项目在F盘,则在F盘根目录建立文件 //如["save\\stud.txt"]则表示在当前项目文件夹里找到命名为[save]的文件夹,把文件新建在该文件夹内 System.out.println("数据已成功写入"); fw.write(str); fw.close(); } catch (Exception e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { try { //操作完要回收流 fw.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { int i,j,k=1; String id[] = new String[5]; int score[] = new int[15]; String name[] = new String[5]; String str=null; Scanner inputId = new Scanner(System.in); Scanner inputName = new Scanner(System.in); Scanner inputScore = new Scanner(System.in); for(i=0;i<5;i++) { System.out.print("请输入第"+(i+1)+"位同学的学号:"); id[i]=inputId.nextLine(); System.out.print("请输入第"+(i+1)+"位同学的姓名:"); name[i]=inputName.nextLine(); for(j=i*3;j<i*3+3;j++) { System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:"); score[j]=inputScore.nextInt(); } k=1; } for(i=0;i<5;i++) { str="学号"+id[i]; str=str+" "+"姓名"+name[i]; for(j=i*3;j<i*3+3;j++) str=str+" "+"第"+k+++"门成绩="+score[j]; k=1; System.out.println(); fileWriter(str+"\r\n"); } } }

解法二:引入Hutool

解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);

2、把学生和成绩拼接成字符串

3、把字符串保存到硬盘文件 "stud.txt "中。

在上一个解法的基础上,优化了第三步,使用

如何详细解析Java将学生成绩输出至磁盘文件的方法?

将String写入文件,UTF-8编码追加模式

FileUtil.appendUtf8String(str,"D:\stud2.txt");

代码详解

public class Basics102_2 { public static void main(String[] args) { int i,j,k=1; String id[] = new String[5]; int score[] = new int[15]; String name[] = new String[5]; String str=null; Scanner inputId = new Scanner(System.in); Scanner inputName = new Scanner(System.in); Scanner inputScore = new Scanner(System.in); for(i=0;i<5;i++) { System.out.print("请输入第"+(i+1)+"位同学的学号:"); id[i]=inputId.nextLine(); System.out.print("请输入第"+(i+1)+"位同学的姓名:"); name[i]=inputName.nextLine(); for(j=i*3;j<i*3+3;j++) { System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:"); score[j]=inputScore.nextInt(); } k=1; } for(i=0;i<5;i++) { str="学号"+id[i]; str=str+" "+"姓名"+name[i]; for(j=i*3;j<i*3+3;j++) str=str+" "+"第"+k+++"门成绩="+score[j]; str +="\n"; k=1; try { //不需要关闭文件流,源码已经有了 FileUtil.appendUtf8String(str,"D:\\stud2.txt"); }catch (IORuntimeException e){ //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); } } } }

到此这篇关于Java实现学生成绩输出到磁盘文件的方法详解的文章就介绍到这了,更多相关Java成绩输出到磁盘文件内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!