zoukankan      html  css  js  c++  java
  • 50 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,

    题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,
    把原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。

     1     public class _50AvgandGrade {
     2 
     3     public static void main(String[] args) {
     4         avgandGrade();
     5     }
     6 
     7     private static void avgandGrade() {
     8         Scanner ss = new Scanner(System.in);
     9         String[][] a = new String[5][6];
    10         for (int i = 1; i < 6; i++) {
    11             System.out.print("请输入第" + i + "个学生的学号:");
    12             a[i - 1][0] = ss.nextLine();
    13             System.out.print("请输入第" + i + "个学生的姓名:");
    14             a[i - 1][1] = ss.nextLine();
    15             for (int j = 1; j < 4; j++) {
    16                 System.out.print("请输入该学生的第" + j + "个成绩:");
    17                 a[i - 1][j + 1] = ss.nextLine();
    18             }
    19             System.out.println("
    ");
    20         }
    21         // 以下计算平均分
    22         float avg;
    23         int sum;
    24         for (int i = 0; i < 5; i++) {
    25             sum = 0;
    26             for (int j = 2; j < 5; j++) {
    27                 sum = sum + Integer.parseInt(a[i][j]);
    28             }
    29             avg = (float) sum / 3;
    30             a[i][5] = String.valueOf(avg);
    31         }
    32         // 以下写磁盘文件
    33         String s1;
    34         try {
    35             File f = new File("C:\stud");
    36             if (f.exists()) {
    37                 System.out.println("文件存在");
    38             } else {
    39                 System.out.println("文件不存在,正在创建文件");
    40                 f.createNewFile();// 不存在则创建
    41             }
    42             BufferedWriter output = new BufferedWriter(new FileWriter(f));
    43             for (int i = 0; i < 5; i++) {
    44                 for (int j = 0; j < 6; j++) {
    45                     s1 = a[i][j] + "
    ";
    46                     output.write(s1);
    47                 }
    48             }
    49             output.close();
    50             System.out.println("数据已写入c盘文件stud中!");
    51         } catch (Exception e) {
    52             e.printStackTrace();
    53         }
    54 
    55     }
    56 
    57 }
  • 相关阅读:
    NSIS打包electron程序为exe安装包
    3、electron打包生成exe文件
    7、Shiro加密和加盐
    6、Shiro之自定义realm
    5、Shiro之jdbcRealm认证授权
    Jmeter-提取Json数据进行关联
    CentOS7 adb
    centos 7 安装pip和pip3
    最新Centos7安装python3并与python2共存
    centos7安装chrome浏览器
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/6544566.html
Copyright © 2011-2022 走看看