JAVA1 연습 & 숙제 1
문제
class VaruableStydy
{
public static void main(String[] args)
{
int kor = 81; // 국어 점수
int eng = 75; // 영어 점수
int mat = 91; // 수학 점수
// 국어 : 81점
// 영어 : 75점
// 수학 : 91점
// 총점 : ---점
// 평균 : --.-점 // 단 소수점이 출력되도록
}
}
풀이...
이게 정말 될줄은 꿈에도 몰랐네...
이런 방식은 추천하고 싶지 않다..
class VaruableStydy
{
public static void main(String[] args)
{
int kor = 81; // 국어 점수
int eng = 75; // 영어 점수
int mat = 91; // 수학 점수
System.out.println( "국어 : " + kor + "점" );
System.out.println( "영어 : " + eng + "점" );
System.out.println( "수학 : " + mat + "점" );
System.out.println( "총점 : " + (kor + eng + mat) );
System.out.println( "평균 : " + ( ((double)kor + eng + mat) ) / 3 );
}
}
안좋은 다른 예
한가지 문장(?)이 반복되면 나중에 수정할때 피곤해진다.
class VaruableStydy
{
public static void main(String[] args)
{
int kor = 81; // 국어 점수
int eng = 75; // 영어 점수
int mat = 91; // 수학 점수
int tot = kor + eng + mat;
double avg = (kor + eng + mat) / 3.0;
System.out.println( "국어 : " + kor + "점" );
System.out.println( "영어 : " + eng + "점" );
System.out.println( "수학 : " + mat + "점" );
System.out.println( "총점 : " + tot + "점" );
System.out.println( "평균 : " + avg + "점" );
}
}
class VaruableStydy
{
public static void main(String[] args)
{
int kor = 81; // 국어 점수
int eng = 75; // 영어 점수
int mat = 91; // 수학 점수
System.out.println( "국어 : " + kor + "점" );
System.out.println( "영어 : " + eng + "점" );
System.out.println( "수학 : " + mat + "점" );
System.out.println( "총점 : " + (kor + eng + mat) + "점" );
System.out.println( "평균 : " + (kor + eng + mat) / 3.0 + "점" );
}
}
만점에 가까운 답
class VaruableStydy
{
public static void main(String[] args)
{
int kor = 81; // 국어 점수
int eng = 75; // 영어 점수
int mat = 91; // 수학 점수
int tot = kor + eng + mat;
double avg = tot / 3.0;
System.out.println( "국어 : " + kor + "점" );
System.out.println( "영어 : " + eng + "점" );
System.out.println( "수학 : " + mat + "점" );
System.out.println( "총점 : " + tot + "점" );
System.out.println( "평균 : " + avg + "점" );
}
}