JAVA 기초 정리/JAVA

5. 연습문제 : 평균성적순위계산기

SummerEda 2019. 12. 19. 10:21
연습 문제

자바테스트.docx
0.05MB

풀이
import java.util.ArrayList;
import java.util.Scanner;

public class CalculMenu {
 //Switch Case로 메뉴를 잡아준다

 Scores arr[];
 ArrayList<Scores> list = new ArrayList<Scores>();
 CalculManage cm = new CalculManage();  
 Scanner sc = new Scanner(System.in);
 Scores sco = new Scores();

 
 public void display() {
   int selectedNo  = 0;  
   boolean mainFlag  = false;

  do{
   System.out.print("메뉴를 선택하세요. (1. 조회 / 2. 입력 / 3. 1등) : ");
   selectedNo = sc.nextInt();
   String num = null;
   
   switch(selectedNo) {
   case 1:
    if(list.size() > 0){
     cm.outputNo();
     }else{  
     System.out.println("입력된 점수가 없습니다.");
     System.out.println(list.size());
     break;    
     }
   case 2:
    cm.inputNo();
    break;
   case 3:
    cm.rankingNo();
    break;
   default:
    System.out.println("존재하지않는 메뉴입니다.");
    break;
   }
  }
  while(!mainFlag);
  sc.close();
 }

}

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class CalculManage {
 
 ArrayList<Scores> list = new ArrayList<Scores>(); 
 int i = 0;
 Scanner sc = new Scanner(System.in);
 Scores sco = new Scores();

 
 public void display() {
   int selectedNo  = 0;  
   boolean mainFlag  = false;

  do{
   System.out.print("메뉴를 선택하세요. (1. 조회 / 2. 입력 / 3. 1등) : ");
   selectedNo = sc.nextInt();
   
   switch(selectedNo) {
   case 1:
    if(list.size() > 0){
     outputNo();
     break;
     }else{  
     System.out.println("입력된 점수가 없습니다.");
     break;    
     }
   case 2:
    inputNo();
    break;
   case 3:
    rankingNo();
    break;
   default:
    System.out.println("존재하지않는 메뉴입니다.");
    break;
   }
  }
  while(!mainFlag);
  sc.close();
 }

 
 
 public void outputNo() {

  Scores sco = new Scores();
  
  System.out.println("번호"+ '\t' + "국어"+ '\t' + "수학 "+ '\t' + "영어"+ '\t'+ "평균");
  for(int i=0;i<list.size();i++) {
//   sco = list.get(i);
//   System.out.println(sco.getstudentname() + " " + sco.getKscore() + " " + sco.getMscore()+ " " + sco.getEscore()+ " " + sco.getAvg());
  System.out.println(list.get(i));
 // break;
  }
  }  
   
 

 public void inputNo () {
      Scanner sc = new Scanner(System.in);
   Scores sco = new Scores(); 
 //  int i = 0;
     
    for(int j =0;j<100;i++) { 
        System.out.println((i + 1) + "번 학생의 국어 점수 : " );
        sco.setKscore(sc.nextInt());
        System.out.println((i + 1) + "번 학생의 수학 점수 : " );
        sco.setMscore(sc.nextInt());
        System.out.println((i + 1) + "번 학생의 영어 점수 : " );
        sco.setEscore(sc.nextInt());
        sco.setAvg(sco.getKscore()+sco.getEscore()+sco.getMscore());
        sco.setstudentname(i + 1);
        list.add(sco); 
          ++i;
        break;
         } 
 }


 
 public void rankingNo() {
  Scores sco = new Scores();
  Scores maxSco = new Scores();
  // list.add(sco);
  double max = 0;
  
  for(i=0;i<list.size();i++) {
   sco = list.get(i);
   if( max < sco.getAvg()) {
    max = sco.getAvg();
    maxSco = list.get(i);
 //   System.out.println("if max : " + max);
 //   System.out.println("if avg : " + sco.getAvg());

    //max = sco.getAvg();
 //     }
 //  else {
 //      System.out.println("else max : " + max);
 //   System.out.println("else avg : " + sco.getAvg());
      }
  }
  System.out.println("번호"+ '\t' + "국어"+ '\t' + "수학 "+ '\t' + "영어"+ '\t'+ "평균");
  System.out.println(maxSco.getstudentname() + " \t " + maxSco.getKscore() + " \t " + maxSco.getMscore()+ " \t " + maxSco.getEscore()+ " \t " + maxSco.getAvg());
 }
}

 

public class CalculMain {

 public static void main(String[] args)  throws Exception {
  // TODO Auto-generated method stub
  CalculManage menu = new CalculManage();
  menu.display();
  
 }

}

 

public class Scores {
 //field
  public int studentname;
  public double kscore;
  public double mscore;
  public double escore;
  public int ranking;
  public double avg;
  public int total;

 //setter getter 
  @Override
  public String toString() {
   return ""  + studentname + " \t " + kscore + " \t " + mscore + " \t " + escore
     + "\t " + avg + " \t  ";
  }
  public int getstudentname() {
   return studentname;
  }
  public void setstudentname(int studentname) {
   this.studentname = studentname;
  }
  public double getKscore() {
   return kscore;
  }
  public void setKscore(double kscore) {
   this.kscore = kscore;
  }
  public double getMscore() {
   return mscore;
  }
  public void setMscore(double mscore) {
   this.mscore = mscore;
  }
  public double getEscore() {
   return escore;
  }
  public void setEscore(double escore) {
   this.escore = escore;
  }
  public int getRanking() {
   return ranking;
  }
  public void setRanking(int ranking) {
   ranking = 1 ;
  }
  public void setRankingplus(int ranking) {
   ranking++;  
  }
  public int getTotal() {
   return total;
  }
  public void setTotal(int total) {
   this.total = total;
  }
  
  public double getAvg() {
   avg = (kscore + mscore + escore) / 3.f;
   return avg;
  }
  public void setAvg(double avg) {
   this.avg = (kscore + mscore + escore) / 3;
  }

}

 

결과