개발/코딩

성적입력(최종)

창훈e 2013. 9. 26. 16:59

package struct;

import java.util.Scanner;

import function.Record;

public class Program3 {
 static int inputMenu(){
  int menu;
  Scanner in = new Scanner(System.in);
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           메인 메뉴                             │");
  System.out.println("└─────────────────────────────┘");
  System.out.println("1.성적 입력");
  System.out.println("2.성적 출력");
  System.out.println("3.종료");
  System.out.print("선택>");
  menu = in.nextInt();
  return menu;
 }

 static int inputRecord(Record[] record,int current){
  
  int kor,eng,math;
  Scanner a = new Scanner(System.in);
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           성적 입력                             │");
  System.out.println("└─────────────────────────────┘");
  
  while(true)   //입력을 계속 하기위한 반복문
  {
  do {
   System.out.print("국어:");
   kor = a.nextInt();

   if (kor < 0 || kor > 100)
    System.out.println("성적 범위를 벗어났습니다,다시 입력하세요");
  } while (kor < 0 || kor > 100);

  do {
   System.out.print("영어:");
   eng = a.nextInt();

   if (eng < 0 || eng > 100)
    System.out.println("성적 범위를 벗어났습니다,다시 입력하세요");
  } while (eng < 0 || eng > 100);

  do {
   System.out.print("수학:");
   math = a.nextInt();

   if (math < 0 || math > 100)
    System.out.println("성적 범위를 벗어났습니다,다시 입력하세요");
  } while (math < 0 ||math > 100);
  
  record[current]=new Record();  //배열원소의 객체 생성,record 배열
  record[current].kor=kor;
  record[current].eng=eng;
  record[current].math=math;
  current ++;
  
  System.out.println("계속 입력하시겠습니까?(1,0)>");
  int isContinue=a.nextInt();
  if(isContinue==1)
  {
   continue;
  }
  else
   return current;
 }
  
 }
 
 static void printRecord(Record[] record,int current){
 
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           성적 출력                             │");
  System.out.println("├────┬────┬────┬────┬────┬────┤");
  System.out.println("│ 번호 │ 국어 │ 영어 │ 수학 │ 총점 │ 평균 │");
  for (int i = 0; i < current; i++) {
   Record A = record[i]; //리턴값은 하나만 받을수 있기 때문에, 한번에 값을 가져오기위해 선언
   int total = A.kor + A.eng + A.math;   //total, avg는 자체적으로 가능하고
   float avg = total / 3.0f;      //kor,eng,math는 서로 공유 하고 있기때문에
   System.out.println("├────┼────┼────┼────┼────┼────┤");
   System.out.printf("│%3d │%3d │%3d │%3d │%3d │%6.2f │\n",i + 1, A.kor, A.eng, A.math, total, avg);
  
  }
  System.out.println("└────┴────┴────┴────┴────┴────┘"); 
 }
 
 public static void main(String[] args) {
  Scanner a = new Scanner(System.in);
  Scanner in = new Scanner(System.in);
  int current=0;  //현재 레코드에 입력이 없다.
  Record[] record = new Record[3];   //Record 참조변수 3개 생성
  int total = 0;
  float avg = 0;
  int menu = 0;
  
  뿅:while (true)
  {    
   switch (inputMenu())
   {   
   case 1:
    current=inputRecord(record,current); //current 값. 몇개 입력받았는지 return.
    break;
   case 2:
    printRecord(record,current);
    break; 
   case 3:
    System.out.println("종료됩니다");
    break 뿅;    
   default:
    System.out.println("입력이 잘못됐습니다. 다시 입력하세요");
   }
  }
 }
}

 

 

 

package struct;   리턴은 하나만 반환할 수 있으므로 클래스를 별도로 선언했다.한번에 담을수 있는 곳.

public class Record {
 int kor;
 int eng;
 int math;
}