国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 創建自定義數組

2019-11-26 14:05:28
字體:
來源:轉載
供稿:網友

1.java創建自定義類數組方法:

Student []stu = new Student[3];for(int i = 0; i < 3; i ++){stu[i] = new Student();}

2.否則會提示空指針異常

package project; import java.io.*;import java.util.Scanner;class Student{  private int id;  private String name;  private int score;     public void setId(int id)  {    this.id = id;  }  public int getId()  {    return this.id;  }  public void setName(String name)  {    this.name = name;  }  public String getName()  {    return this.name;  }  public void setScore(int score)  {    this.score = score;  }  public int getScore()  {    return this.score;  }}public class project2 {  File file = new File("E:/data.txt");  FileWriter filewrite = null;  BufferedWriter write = null;  FileReader fileread = null;  BufferedReader read = null;  Student []stu = new Student[3];  public void put()  {    try {      filewrite = new FileWriter(file);    } catch (IOException e) {      // TODO 自動生成的 catch 塊      e.printStackTrace();    }    write = new BufferedWriter(filewrite);    for(int i = 0; i < 3; i ++)    {      System.out.println("請輸入第" + (i + 1) + "個學生的ID,姓名,成績:");      Scanner in = new Scanner(System.in);      try {        String str = in.nextLine();        String data[] = str.split(" ");        for(int j = 0; j < 3; j++)        {          write.write(data[j]);          write.newLine();        }               } catch (IOException e) {        // TODO 自動生成的 catch 塊        e.printStackTrace();      }           }    try {      write.close();      filewrite.close();    } catch (IOException e) {      // TODO 自動生成的 catch 塊      e.printStackTrace();    }  }        public void get()  {    int sum = 0;    double ave;    try {      fileread = new FileReader(file);    } catch (FileNotFoundException e) {      // TODO 自動生成的 catch 塊      e.printStackTrace();    }    read = new BufferedReader(fileread);    for(int i = 0; i < 3; i ++)    {      stu[i] = new Student();      try {        stu[i].setId(Integer.parseInt(read.readLine()));        stu[i].setName(read.readLine());        stu[i].setScore(Integer.parseInt(read.readLine()));      } catch (Exception e) {        // TODO 自動生成的 catch 塊        e.printStackTrace();      }    }         for(int i = 0; i < 3; i ++)    {      sum += stu[i].getScore();    }    ave = sum * 1.0/3;    System.out.println("學生的平均成績為:" + ave);    try {      read.close();      fileread.close();    } catch (IOException e) {      // TODO 自動生成的 catch 塊      e.printStackTrace();    }  }  public static void main (String []args)  {    project2 pro = new project2();    pro.put();    pro.get();  }}

    總結:

             這樣我們就可以在項目當中,根據項目需求自己來定義想要的數組.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大石桥市| 巴彦淖尔市| 定边县| 茂名市| 芦山县| 庆城县| 仲巴县| 驻马店市| 施秉县| 民乐县| 衡东县| 苏尼特右旗| 林周县| 肥东县| 普格县| 万盛区| 油尖旺区| 肃北| 正镶白旗| 介休市| 平湖市| 扬中市| 宝应县| 泾川县| 仁化县| 齐齐哈尔市| 凉山| 伊金霍洛旗| 合水县| 象山县| 大港区| 林州市| 太仆寺旗| 宣恩县| 台中县| 澎湖县| 丰宁| 宝兴县| 开封县| 灌云县| 长丰县|