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();
}
}
요약:이렇게 하면 우리는 프로젝트에서 프로젝트의 수요에 따라 원하는 그룹을 정의할 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.