【Java】实验七
1. 编写程序,在控制台窗口提示输入两个整数,然后接收这两个整数,并输出它们的和。(要求:键盘输入通过流封装System.in获取,不要使用Scanner类)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.ly.stream; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 6 public class InputStreamTest { 7 public static void main(String[] args) { 8 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 9 String line=null; 10 try{ 11 System.out.print("请输入第一个正整数a:"); 12 line = in.readLine(); 13 int a = Integer.parseInt(line); 14 System.out.print("请输入第二个正整数b:"); 15 line = in.readLine(); 16 int b = Integer.parseInt(line); 17 System.out.println("a+b=" + String.valueOf(a+b)); 18 }catch(Exception e){ 19 System.out.println("输入错误!"); 20 System.exit(0); 21 } 22 } 23 }
2. 设计学生类Student,属性:编号(整型);姓名(字符串),成绩(整型)。编写一个程序:要求:(1)输入3个学生的姓名和成绩,将其姓名和成绩保存到data.txt中;(2)然后从该文件中读取数据,求得这五个学生的平均成绩。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.ly.file; 2 3 public class Student { 4 int id; 5 String name; 6 int score; 7 8 Student(){} 9 public void setId(int id){ 10 this.id = id; 11 } 12 13 public void setName(String name){ 14 this.name = name; 15 } 16 17 public void setScore(int score){ 18 this.score = score; 19 } 20 21 public String toString(){ 22 return this.id + "\t" + this.name + "\t" + this.score + "\n"; 23 } 24 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.ly.file; 2 3 import java.io.BufferedReader; 4 import java.io.DataInputStream; 5 import java.io.DataOutputStream; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 12 public class Main { 13 public static void main(String[] args) { 14 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 15 try{ 16 DataOutputStream out = new DataOutputStream(new FileOutputStream("D:\\student.txt")); 17 String line; 18 for(int i=0;i<3;i++){ 19 Student stu = new Student(); 20 21 System.out.print("学号:"); 22 line = in.readLine(); 23 int id = Integer.parseInt(line); 24 stu.setId(id); 25 26 System.out.print("姓名:"); 27 String name = in.readLine(); 28 stu.setName(name); 29 30 System.out.print("成绩:"); 31 line = in.readLine(); 32 int score = Integer.parseInt(line); 33 stu.setScore(score); 34 35 out.writeBytes(stu.toString()); 36 } 37 out.close(); 38 }catch(IOException e){ 39 System.out.println("文件写入失败!"); 40 System.exit(0); 41 } 42 43 try{ 44 DataInputStream din = new DataInputStream(new FileInputStream("D:\\student.txt")); 45 int ScoreSum = 0; 46 String line; 47 for(int i=0;i<3;i++){ 48 line = din.readLine(); 49 int score = Integer.parseInt(line.trim().split("\t")[2]); 50 ScoreSum += score; 51 } 52 System.out.println("平均成绩:" + ScoreSum/3); 53 }catch(FileNotFoundException e){ 54 System.out.println("文件不存在!"); 55 System.exit(0); 56 }catch(IOException e){ 57 System.out.println("文件读取失败!"); 58 System.exit(0); 59 } 60 } 61 }