본문 바로가기

Java

메인메소드의 기능들을 각 메소드별로 분리하기

import java.util.Scanner;
 
public class noname {
 
    public static void main(String[] args) {
        
        int[] arr = new int[3];
        
        int total = 0;
        float avg;
        int menu;
        boolean Loop = true;
        
        Scanner scan = new Scanner(System.in);
        
        while(Loop)
        {
            System.out.println("      -메인 메뉴-");
            System.out.println("\t1. 성적입력 ");
            System.out.println("\t2. 성적출력 ");
            System.out.println("\t3. 종료 ");
            System.out.println("\t선택> ");
            menu = scan.nextInt();
            
            switch(menu)
            {
            case 1:
                
                System.out.println("      -성적 입력-");
                System.out.println();
                
                for(int i = 0; i < 3; i++)
                {
                    do 
                    {
                        System.out.printf("국어%d : ", i+1);
                        arr[i] = scan.nextInt();
                        
                        if(arr[i] < 0 || 100 < arr[i])
                        {
                            System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
                        }
                    }while(arr[i]<0 || 100 < arr[i]);
                }
                
                System.out.println("────────────────────────");
                break;
            case 2:
                for(int i = 0; i < 3; i++)
                {
                    total += arr[i];
                }
                avg = total / 3.0f;
                
                System.out.println("      -성적 출력-");
                System.out.println();
                
                for(int i = 0; i < 3; i++)
                {
                    System.out.printf("국어 %d : %3d\n", i+1, arr[i]);
                }
                System.out.printf("총점 : %3d\n", total);
                System.out.printf("평균 : %6.2f\n", avg);
                System.out.println("────────────────────────");
                
                break;
            case 3:
                System.out.println("-프로그램 출력-");
                
                Loop = false;
                break;
                
            default:
                System.out.println("잘못된 값을 입력하셨습니다. 메뉴는 1~3까지입니다.");
            }
        }
 
    }
 
}
 
 
cs

 

 

main메소드 하나에 모든 기능을 구현한 기본형.

 

 

import java.util.Scanner;
public class MethodSplit {
    static int[] arr = new int[3];
    
    public static void main(String[] args) throws Exception {
        boolean loop = true;
        
        while(loop)
        {
            int m = menu();
            switch(m)
            { 
            case 1:
                inputScore();
                break;
            case 2:
                outputScore();
                break;
            case 3:
                System.out.println("프로그램 종료.");
                
                loop = false;
                break;
                
            default:
                System.out.println("잘못된 값을 입력하셨습니다. 메뉴는 1~3까지입니다.");
            }
        }
    }
    
    static void inputScore() {
        Scanner sc = new Scanner(System.in);
 
        System.out.println("      -성적 입력-");
        System.out.println();
 
        for (int i = 0; i < 3; i++) {
            do {
                System.out.printf("국어%d : ", i + 1);
                arr[i] = sc.nextInt();
 
                if (arr[i] < 0 || 100 < arr[i]) {
                    System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
                }
            } while (arr[i] < 0 || 100 < arr[i]);
        }
 
        System.out.println("────────────────────────");
        
    }
    static void outputScore() {
        int total = 0;
        float avg;
 
        for(int i = 0; i < 3; i++)
                {
                    total += arr[i];
                }
                avg = total / 3.0f;
                
                System.out.println("      -성적 출력-");
                System.out.println();
                
                for(int i = 0; i < 3; i++)
                {
                    System.out.printf("국어 %d : %3d\n", i+1, arr[i]);
                }
                System.out.printf("총점 : %3d\n", total);
                System.out.printf("평균 : %6.2f\n", avg);
                System.out.println("────────────────────────");
    }
    static int menu() {
            Scanner sc = new Scanner(System.in);
            System.out.println("      -메인 메뉴-");
            System.out.println("\t1. 성적입력 ");
            System.out.println("\t2. 성적출력 ");
            System.out.println("\t3. 종료 ");
            System.out.println("\t선택> ");
            int menu = sc.nextInt();
            return menu;
    }
}
 
 
cs

 

입/출력기능을 각각의 메소드로 분리

 

 

- checklist

총합과 평균은 출력부에서만 사용되므로 해당 변수들은 outputScore()의 지역변수로

Scanner의 인스턴스는 실행될때마다 다른 값을 받기 때문에 각각의 메소드에서 생성.

메뉴입력부, 성적입/출력부를 각각의 메소드로 분할 main메소드의 지역변수였던 arr을 전역변수로 이동