성적입력(최종)

개발/코딩 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;
}

성적관리->> 함수 쓰고, 클래스 써서

개발/코딩 2013. 9. 25. 18:53

package struct;

import java.util.Scanner;

import function.Record;

public class Program2 {
 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 void inputRecord(Record record){  // 변수3개 모두 리턴으로 넘기지 못하기때문에 클래스를 선언한다.
  int kor,eng,math;
  Scanner a = new Scanner(System.in);
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           성적 입력                             │");
  System.out.println("└─────────────────────────────┘");

  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.kor=kor;
  record.eng=eng;
  record.math=math;
  
 }
 
 static void printRecord(Record record){
  int total = record.kor + record.eng + record.math;   //total, avg는 자체적으로 가능하고
  float avg = total / 3.0f;      //kor,eng,math는 서로 공유 하고 있기때문에
  System.out.println("┌─────────────────────────────┐");
  System.out.println("│           성적 출력                             │");
  System.out.println("├────┬────┬────┬────┬────┬────┤");
  System.out.println("│ 번호 │ 국어 │ 영어 │ 수학 │ 총점 │ 평균 │");
  for (int i = 0; i < 3; i++) {
   System.out.println("├────┼────┼────┼────┼────┼────┤");
   System.out.printf("│%3d │%3d │%3d │%3d │%3d │%6.2f │\n",i + 1, record.kor, record.eng, record.math, total, avg);
  }
  System.out.println("└────┴────┴────┴────┴────┴────┘"); 
 }
 
 public static void main(String[] args) {
  Record record = new Record();
  int total = 0;
  float avg = 0;
  int menu = 0;
  Scanner a = new Scanner(System.in);
  Scanner in = new Scanner(System.in);
  뿅:while (true) {
    
   switch (inputMenu()) {
   
   case 1:
   inputRecord(record);
    break;

   case 2:
    printRecord(record);
    break;
    
   case 3:
    System.out.println("종료됩니다");
    break 뿅;
    
   default:
    System.out.println("입력이 잘못됐습니다. 다시 입력하세요");
   }
  }
 }
}

public class Record{      //class 선언

int kor;

int eng;

int math;

}

 

 

 

함수쓰는거 업그레이드

개발/코딩 2013. 9. 24. 17:23

package Array;

import java.util.Random;

public class Program {

 static void genLotto(int[] lotto) {
  Random rand = new Random();
  for (int i = 0; i < 6; i++)
   lotto[i] = rand.nextInt(45) + 1;
 }
 static void printLotto(int[] lotto){
  for (int i = 0; i < 6; i++)
   System.out.printf("%d ", lotto[i]);
 }
 static void lineLotto(int[]lotto){
  Random rand = new Random();
  for (int k = 0; k < 5; k++)
  {
   for (int i = 0; i < 5; i++) // 큰 수를 뒤로 배열하고 중복되는 수가 있다면 새로 출력하는 과정
   {
    if (lotto[i] > lotto[i + 1]) // 큰 수를 뒤로
    {
     int t;
     t = lotto[i];
     lotto[i] = lotto[i + 1];
     lotto[i + 1] = t;
    }

    else if (lotto[i] == lotto[i + 1]) // 만약 두 수가 같다면
    {
     System.out.println("중복"); // '중복' 출력
     lotto[i + 1] = rand.nextInt(45) + 1; // 뒤에 있는 수를 새로 출력
    }
   }
  }
  for (int i = 0; i < 6; i++)
   System.out.printf("%d ", lotto[i]);
  
 }
 
 public static void main(String[] args) {

  Random rand = new Random();
  int[] lotto = new int[6];  

  genLotto(lotto);
  
  /*for (int i = 0; i < 6; i++)
   System.out.printf("%d ", lotto[i]);*/
  printLotto(lotto);
  
  System.out.println();
  /*for (int k = 0; k < 5; k++)
  {
   for (int i = 0; i < 5; i++) // 큰 수를 뒤로 배열하고 중복되는 수가 있다면 새로 출력하는 과정
   {
    if (lotto[i] > lotto[i + 1]) // 큰 수를 뒤로
    {
     int t;
     t = lotto[i];
     lotto[i] = lotto[i + 1];
     lotto[i + 1] = t;
    }

    else if (lotto[i] == lotto[i + 1]) // 만약 두 수가 같다면
    {
     System.out.println("중복"); // '중복' 출력
     lotto[i + 1] = rand.nextInt(45) + 1; // 뒤에 있는 수를 새로 출력
    }
   }
  }
  for (int i = 0; i < 6; i++)
   System.out.printf("%d ", lotto[i]); // 중복되지 않은 배열을 출력*/
   lineLotto(lotto);
 }
}

함수쓰느너

개발/코딩 2013. 9. 24. 16:07

package function;

public class Function {

 static int add(int x,int y)
 {
    return x+y; //int sum 에 반환하는 값
 }
 public static void main(String[] args){
  
  int x=3;
  int y=4;
  int sum=add(x,y);
  
  System.out.printf("%d",sum);
 }
}


 

정수 10개를 입력받아 배열에 저장후 큰순서 대로 프린트

개발/코딩 2013. 9. 24. 10:26

package Array;

import java.util.Scanner;

public class Program {
 public static void main(String[] args){
  Scanner in=new Scanner(System.in);
  int[]A=new int[10];
  int i,j,temp=0;
  for(i=0;i<A.length;i++){    //임의의 정수 입력받음
   A[i]=in.nextInt();  
  }
  
  for(i=0;i<A.length;i++){    //순차적으로 배치
  for(j=i+1;j<10;j++){
   if(A[i]>A[j]){
   temp=A[i];
   A[i]=A[j];
   A[j]=temp;
   }
  }
  System.out.printf("%3d",A[i]);
 }
}
}

'개발 > 코딩' 카테고리의 다른 글

함수쓰는거 업그레이드  (0) 2013.09.24
함수쓰느너  (0) 2013.09.24
로또 중복안되게...짜증나네  (0) 2013.09.23
달력 만들기  (0) 2013.09.23
lotto  (0) 2013.09.17

로또 중복안되게...짜증나네

개발/코딩 2013. 9. 23. 18:34

package Array;

import java.util.Random;

public class Program {
 public static void main(String[] args) {
  Random rand = new Random(); // 랜덤 함수
  int[] Lotto = new int[6]; // 배열 선언
  for (int i = 0; i < Lotto.length; i++) {
   int A=rand.nextInt(45) + 1; // 배열선언시 0부터 시작하므로 +1
   for(int j=0;j<Lotto.length;j++){
    if(A==Lotto[j]){
     A=rand.nextInt(45) + 1; // 배열선언시 0부터 시작하므로 +1
     j = -1;          //j++ 값을 상쇄시켜주기 위해(증감을 막음. 비교하기 위해서)
    }
   }
   Lotto[i]=A;
   System.out.printf("%d ", Lotto[i]); // 로또 랜덤 6개 출력
  }
  System.out.println(); // 띄어쓰기 프린트

  for (int i = 0; i < 6; i++) {
   for (int k = i + 1; k < 6; k++) {
    if (Lotto[i] > Lotto[k]) { // 두수를 비교해서 i값이 크다면
     int temp;
     temp = Lotto[i];
     Lotto[i] = Lotto[k];
     Lotto[k] = temp; // 큰숫자를 뒤로 보낸다
    }
   }

   System.out.printf("%d ", Lotto[i]);
  }
 }
}

'개발 > 코딩' 카테고리의 다른 글

함수쓰느너  (0) 2013.09.24
정수 10개를 입력받아 배열에 저장후 큰순서 대로 프린트  (0) 2013.09.24
달력 만들기  (0) 2013.09.23
lotto  (0) 2013.09.17
성적 입력(switch)  (0) 2013.09.17

달력 만들기

개발/코딩 2013. 9. 23. 17:31

package calendar;

import java.util.Calendar;
import java.util.Scanner;

public class Program {
 public static void main(String[] args) {
  int[] days = new int[42];
  Scanner in = new Scanner(System.in);
  while (true) {
   System.out.println("원하는 년도의 월을 입력하시오");
   Calendar cal = Calendar.getInstance();
   int curYear = cal.get(Calendar.YEAR);
   int curMonth = cal.get(Calendar.MONTH) + 1;// Month가 0부터 시작한다.
   curYear = in.nextInt(); // 년도 입력
   System.out.print("년");
   curMonth = in.nextInt(); // 월 입력
   System.out.print("월");
   cal.set(curYear, curMonth - 1, 1);
   int week = cal.get(Calendar.DAY_OF_WEEK);
   int start = 2 - week;// 각 월의 1일에 해당하는 요일 구하기
   int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 해당 달의 마지막

   /* System.out.printf("%d\n", week); */

   for (int i = 0, n = start; i < 42; i++, n++)
    if (1 <= n && n <= end) {
     days[i] = n;
    } else
     days[i] = 0; // end 날짜 이외는 0으로 표시

   System.out.printf("<%d년%d월>", curYear, curMonth);

   for (int i = 0; i < 42; i++) {
    if (i % 7 == 0) { // 일주일 표시
     System.out.println();
    }
    System.out.printf("%d\t", days[i]);
   }
  }
 }
}

'개발 > 코딩' 카테고리의 다른 글

정수 10개를 입력받아 배열에 저장후 큰순서 대로 프린트  (0) 2013.09.24
로또 중복안되게...짜증나네  (0) 2013.09.23
lotto  (0) 2013.09.17
성적 입력(switch)  (0) 2013.09.17
성적 계속 입력 (do-while문)  (0) 2013.09.16

lotto

개발/코딩 2013. 9. 17. 17:54

import java.util.Random;

public class 연습 {
 public static void main(String[] args) {
  Random rand = new Random(); //랜덤 함수
  int[] Lotto = new int[6];  //배열 선언
  for (int i = 0; i < Lotto.length; i++) {
   Lotto[i] = rand.nextInt(45) + 1; //배열선언시 0부터 시작하므로 +1
   System.out.printf("%d ", Lotto[i]); //로또 랜덤 6개 출력
  }
  System.out.println(); //띄어쓰기 프린

  for (int i = 0; i < 6; i++) {
   for (int k = i + 1; k < 6; k++) {
    if (Lotto[i] > Lotto[k]) { //두수를 비교해서 i값이 크다면
     int temp;
     temp = Lotto[i];
     Lotto[i] = Lotto[k];
     Lotto[k] = temp;     //큰숫자를 뒤로 보낸다
    }
   }
  
   System.out.printf("%d ", Lotto[i]);
  }
 }
}

 

'개발 > 코딩' 카테고리의 다른 글

로또 중복안되게...짜증나네  (0) 2013.09.23
달력 만들기  (0) 2013.09.23
성적 입력(switch)  (0) 2013.09.17
성적 계속 입력 (do-while문)  (0) 2013.09.16
성적계속 입력(if)  (0) 2013.09.13

성적 입력(switch)

개발/코딩 2013. 9. 17. 15:20

import java.util.Scanner;

public class Program2 {
 public static void main(String[] args) {
  int kor = 0;
  int eng = 0;
  int math = 0;
  int total = 0;
  float avg = 0;
  int menu;
  Scanner a = new Scanner(System.in);
  Scanner in = new Scanner(System.in);
  뿅:while (true) {
   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();
   switch (menu) {
   
   case 1:
    System.out.println("┌─────────────────────────────┐");
    System.out.println("│           성적 입력                             │");
    System.out.println("└─────────────────────────────┘");

    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);
    break;

   case 2:
    total = kor + eng + math;
    avg = total / 3.0f;
    System.out.println("┌─────────────────────────────┐");
    System.out.println("│           성적 출력                             │");
    System.out.println("├────┬────┬────┬────┬────┬────┤");
    System.out.println("│ 번호 │ 국어 │ 영어 │ 수학 │ 총점 │ 평균 │");
    for (int i = 0; i < 3; i++) {
     System.out.println("├────┼────┼────┼────┼────┼────┤");
     System.out.printf("│%3d │%3d │%3d │%3d │%3d │%6.2f │\n",i + 1, kor, eng, math, total, avg);
    }
    System.out.println("└────┴────┴────┴────┴────┴────┘");
    break;
   case 3:
    System.out.println("종료됩니다");
    break 뿅;
   default:
    System.out.println("입력이 잘못됐습니다. 다시 입력하세요");
   }
  }
 }
}

'개발 > 코딩' 카테고리의 다른 글

달력 만들기  (0) 2013.09.23
lotto  (0) 2013.09.17
성적 계속 입력 (do-while문)  (0) 2013.09.16
성적계속 입력(if)  (0) 2013.09.13
로또번호 입력  (0) 2013.09.12

성적 계속 입력 (do-while문)

개발/코딩 2013. 9. 16. 15:15

import java.util.Scanner;

public class Program {
 public static void main(String[] args) {
  int kor = 0;
  int eng = 0;
  int math = 0;
  int total = 0;
  float avg = 0;
  int menu;
  Scanner a = new Scanner(System.in);
  Scanner in = new Scanner(System.in);
  while (true) {
   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();

   if (menu == 1) {
    System.out.println("┌─────────────────────────────┐");
    System.out.println("│           성적 입력                             │");
    System.out.println("└─────────────────────────────┘");
  
    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);
    }

   else if(menu==2){
    total = kor + eng + math;
    avg = total / 3.0f;
    System.out.println("┌─────────────────────────────┐");
    System.out.println("│           성적 출력                             │");
    System.out.println("├────┬────┬────┬────┬────┬────┤");
    System.out.println("│ 번호 │ 국어 │ 영어 │ 수학 │ 총점 │ 평균 │");
    System.out.println("├────┼────┼────┼────┼────┼────┤");
    System.out.printf("│%3d │%3d │%3d │%3d │%3d │%6.2f │\n", 1, kor,eng, math, total, avg);
    System.out.println("└────┴────┴────┴────┴────┴────┘");
   }
   else if (menu==3){
    System.out.println("종료됩니다");
   break;
   }
   else{
   System.out.println("입력이 잘못됐습니다. 다시 입력하세요");
   }

  }
 }
}

'개발 > 코딩' 카테고리의 다른 글

lotto  (0) 2013.09.17
성적 입력(switch)  (0) 2013.09.17
성적계속 입력(if)  (0) 2013.09.13
로또번호 입력  (0) 2013.09.12
Continue문 (1~100까지 짝수의 합)  (0) 2013.09.12