본문 바로가기

JAVA/JAVA Programming

[JAVA] 성적 처리 프로그램

반응형
█ 성적 처리 프로그램 (do ~ while)

import java.io.*;
public class Round08_Ex04 {

    public static void main(String[] ar) throws IOException {
       
        BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
        int kor = 0, eng = 0, math = 0, tot = 0;
        float avg = 0.0f;
               
        do {
           
            System.out.print("국어 = ");
            kor = Integer.parseInt(in.readLine());
        } while(kor < 0 || kor > 100);
       
        do {
           
            System.out.print("영어 = ");
            eng = Integer.parseInt(in.readLine());
        } while(eng < 0 || eng > 100);
       
        do {
           
            System.out.print("수학 = ");
            math = Integer.parseInt(in.readLine());
        } while(math < 0 || math > 100);
       
        tot = kor + eng + math;
        avg = tot / 3.0f;
       
        System.out.println();
        System.out.println("총점 = " + tot);
        System.out.println("평균 = " + avg);
    }
}

█ 성적 처리 프로그램 (do ~ while_배열①)

import java.io.*;
public class Round08_Ex04_02 {

    public static void main(String[] ar) throws IOException {
       
        BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
        //int kor = 0, eng = 0, math = 0, tot = 0;
        int[] sub = new int[4];    // 0 : 국어, 1 : 영어, 2 : 수학, 3 : 총
        float avg = 0.0f;
               
        do {
           
            System.out.print("국어 = ");
            // kor = Integer.parseInt(in.readLine());
            sub[0] = Integer.parseInt(in.readLine());
        } // while(kor < 0 || kor > 100);
          while(sub[0] < 0 || sub[0] > 100);
       
        do {
           
            System.out.print("영어 = ");
            // eng = Integer.parseInt(in.readLine());
            sub[1] = Integer.parseInt(in.readLine());
        } // while(eng < 0 || eng > 100);
          while(sub[1] < 0 || sub[1] > 100);
       
        do {
           
            System.out.print("수학 = ");
            // math = Integer.parseInt(in.readLine());
            sub[2] = Integer.parseInt(in.readLine());
        } // while(math < 0 || math > 100);
          while(sub[2] < 0 || sub[2] > 100);
       
        // tot = kor + eng + math;
        sub[3] = sub[0] + sub[1] + sub[2];
        // avg = tot / 3.0f;
        avg = sub[3] / 3.0f;
       
        System.out.println();
        // System.out.println("총점 = " + tot);
        System.out.println("총점 = " + sub[3]);
        System.out.println("평균 = " + avg);
    }
}


█ 성적 처리 프로그램 (do ~ while_배열②)

import java.io.*;
public class Round08_Ex04_03 {

    public static void main(String[] ar) throws IOException {
      
        BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
        // int kor = 0, eng = 0, math = 0, tot = 0;
        // int[] sub = new int[4];    // 0 : 국어, 1 : 영어, 2 : 수학, 3 : 총합
        String[] subname = {"국어", "영어", "수학"};
        int[] sub = new int[subname.length + 1];    // 0 : 국어, 1 : 영어, 2 : 수학, 3 : 총합
        float avg = 0.0f;
              
        do {
          
            // System.out.print("국어 = ");
            System.out.print(subname[0] = " = ");
            // kor = Integer.parseInt(in.readLine());
            sub[0] = Integer.parseInt(in.readLine());
        } // while(kor < 0 || kor > 100);
          while(sub[0] < 0 || sub[0] > 100);
      
        do {
          
            System.out.print(subname[1] = " = ");
            // eng = Integer.parseInt(in.readLine());
            sub[1] = Integer.parseInt(in.readLine());
        } // while(eng < 0 || eng > 100);
          while(sub[1] < 0 || sub[1] > 100);
      
        do {
          
            System.out.print(subname[2] = " = ");
            // math = Integer.parseInt(in.readLine());
            sub[2] = Integer.parseInt(in.readLine());
        } // while(math < 0 || math > 100);
          while(sub[2] < 0 || sub[2] > 100);
      
        // tot = kor + eng + math;
        // sub[3] = sub[0] + sub[1] + sub[2];
        sub[sub.length - 1] = sub[0] + sub[1] + sub[2];
        // avg = tot / 3.0f;
        // avg = sub[3] / 3.0f;
        avg = sub[sub.length - 1] / (float)subname.length;
      
        System.out.println();
        // System.out.println("총점 = " + tot);
        // System.out.println("총점 = " + sub[3]);
        System.out.println("총점 = " + sub[sub.length - 1]);
        System.out.println("평균 = " + avg);
    }
}


█ 성적 처리 프로그램 (for_배열)

import java.io.*;
public class Round08_Ex04_04 {

    public static void main(String[] ar) throws IOException {
       
        BufferedReader in = new BufferedReader(new InputStreamReader (System.in));

        String[] subname = {"국어", "영어", "수학"};
        int[] sub = new int[subname.length + 1];    // 0 : 국어, 1 : 영어, 2 : 수학, 3 : 총합
        float avg = 0.0f;
       
        // length : sub배열에 가지고 있는 엘리먼트(값)의 자체가 아닌 가지고 있는 갯수만을 나타낸다.
        // sub.length - 1에서 -1을 해주는 이유는 위 변수선언에서 총합의 값을 가지기 위해 [subname.length + 1];이라
        // 선언을 해두었기 때문에 국어, 영어, 수학 총 3번을 돌아야 하는 for문이기에 '-1'을 해준것이다.
        for(int i = 0; i < sub.length - 1; i++) {
           
            do {
               
                System.out.print(subname[i] + " = ");
                sub[i] = Integer.parseInt(in.readLine());
               
            } while(sub[i] < 0 || sub[i] > 100);    // 0보다 작거나 100보다 크면 do ~ while문을 다시 실
           
            sub[sub.length - 1] += sub[i];
        }
       
        avg = sub[sub.length - 1] / (float)(sub.length - 1);
      
        System.out.println();
        // System.out.println("총점 = " + tot);
        // System.out.println("총점 = " + sub[3]);
        System.out.println("총점 = " + sub[sub.length - 1]);
        System.out.println("평균 = " + avg);
    }
}

반응형

'JAVA > JAVA Programming' 카테고리의 다른 글

[JAVA] A부터 Z까지 나타내기  (0) 2012.01.06
[JAVA] 1부터 임의 값까지의 합  (0) 2012.01.06
[JAVA] Calender 클래스 활용  (0) 2012.01.04
[JAVA] 배열  (0) 2012.01.04
[JAVA] JAVA 메서드  (0) 2012.01.03