반응형
public class StudentTest {//---------10
public static void main(String[] args) {
//국어: 100, 영어:60, 수학:76
//실행결과
//이름: 홍길동
//총점 : 236
//평균 : 78.7 소수점 2번째 자리에서 반올림
Student stu = new Student("김유림",3,17,100,60,76);
int total = stu.getTotal();
double avg = stu.getAverage();
System.out.println("이름 : "+ stu.name);
System.out.println("총점 : "+ total);
System.out.println("평균 : "+ String.format("%.1f", avg));
}
}
public class Student {//---------10
//클래스의 기본 구성 : 변수,생성자,메소드
String name; //학생이름
int ban; //반
int no; //번호
int kor; //국어점수
int eng; //영어점수
int math; //수학점수
Student(String name,int ban,int no,int kor,int eng,int math){
this.name = name;
this.ban = ban;
this.no = no;
this.kor = kor;
this.eng = eng;
this.math = math;
}
int getTotal() { //총점
return kor + eng + math;
}
double getAverage() { //평균
return (double)(getTotal())/3;
}
// double getAverage() { //평균
// int total = getTotal();
// double avg = (double)total / 3; //나눌 값 double형으로 나눠야함
//
// return avg;
//}
// double getAverage() { //평균
// return (int)(getTotal() / 3.0 * 10 +0.5) / 10.0;//나눌 값 3도 double형으로 나눠야함
//
// }
}
반응형
'java' 카테고리의 다른 글
Mac_jdk 환경 변수 설정하기 (0) | 2022.10.02 |
---|---|
Java - Open JDK 1.8 설치 (0) | 2022.10.01 |
0929_초기화블럭 (0) | 2022.09.29 |
0929_this. / this() (0) | 2022.09.29 |
0929_생성자 (0) | 2022.09.29 |