Salted Caramel

[수업 6일차] 21.08.09, Exam01, Exam02, Exam03, method 본문

coding/[2021.08.02~2022.01.24] 수업복습

[수업 6일차] 21.08.09, Exam01, Exam02, Exam03, method

꽃무늬라떼 2021. 8. 10. 02:18
  • Exam02_01
package sist;


/*
 * [문제1] 지방(fat), 탄수화물(car), 단백질(protein)을 
 * 키보드로 입력 받아서 칼로리의 합계를 구하는 프로그램. 
 *  * 총 칼로리 : (지방*9) + (탄수화물*4) + (단백질*4)
 */

import java.util.Scanner;

public class Exam02_01 {

	public static void main(String[] args) {
		
		Scanner sc =new Scanner(System.in);
		
		System.out.print("지방의 그램을 입력하세요 : ");
		
		int fat = sc.nextInt();
		
		System.out.print("탄수화물의 그램을 입력하세요 : ");
		
		int car = sc.nextInt();
		
		System.out.println("단백질의 그램을 입력하세요 : ");
		
		int protein = sc.nextInt();
		
		// 총 칼로리를 구하자.
		// 총 칼로리 : (지방*9) + (탄수화물*4) + (단백질*4)
		
		int total = (fat*9)+(car*4)+(protein*4);
				
		System.out.printf("총칼로리 : %,d cal\n", + total);
		
		sc.close();
		
		
		
		
		
	}

}

 

 


  • Exam02_02
package sist;

import java.util.Scanner;

/*
 * [문제2] 1로부터 사용자가 키보드로 입력한 수까지의 
 * 		   홀수의 합과 짝수의 합을 구하시오. 
 */

public class Exam02_02 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("정수 입력 : ");
	
		int su = sc.nextInt();
		
		int oddSum = 0, evenSum = 0;
		
		for(int i =1; i<= su ; i++) {
			if(i%2 == 1) {
				oddSum += i;
			}else {
				evenSum += i;		
		}
		}
		
		
		System.out.println("홀수의 합 >>> " + oddSum);
		System.out.println("짝수의 합 >>> " + evenSum);
		
		sc.close();
		}

	}


  • Exam02_03

별 출력하는 코드

 

 

삼항연산자

 

package sist;

public class Exam02_03 {

	public static void main(String[] args) {
	
			
				for(int i=1; i<7; i++) { 
					for(int j=1; j<=i; j++) {
						System.out.print("*");
						
					}
					System.out.println();
				}
			
				
				for(int i=6; i>=1; i--) {
					for(int j=1; j<=i; j++) {
						System.out.print("*");
						
					}
					System.out.println();
					}
			}



	}
package sist;

/*
 * [문제3] 다중 for문 예제
 * *
 * **
 * ***
 * ****
 * *****
 * ******
 * *****
 * ****
 * ***
 * **
 * *
 */


public class Exam02_03_01 {

	public static void main(String[] args) {
		
		// 1. 올라가는 별 찍기
		for(int i =1; i<=6; i++) {  // i변수: 라인(행) 수
			for(int j=1; j<=i; j++) { //j변수: 별의(열) 갯수
				System.out.println("*");
			}
			System.out.println();
		}
		//
	}

}
package sist;

import java.util.Scanner;

public class Exam02_03_02 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("별의 최대 갯수를 입력하세요. : ");
		
		int starCount = sc.nextInt();
		
		//전체 line 수는 (입력받은 별의 최대 갯수* 2-1) 이 되어야 함
		
		for(int i=1; i<=(starCount*2 -1); i++) {
			
			int line = (i<=starCount) ? i : (starCount * 2 -i);
			
			for(int j=1; j<=line; j++) {
				System.out.print("*");
				}
				System.out.println();	
		}
		
		sc.close();

	}

}

  • Exam02_04
package sist;

/*
 * [문제4] 역삼각형 모양의 알파벳 
 * 		    도형을 만들어 보세요.
 */


public class Exam02_04 {

	public static void main(String[] args) {
		
        //방법1
		
		for(char c='Z'; c>='A'; c--) { 
			for(char d='A'; d<=c; d++) {
				System.out.print(d);
				
			}
			System.out.println();
		}
	
		System.out.println();
		
        
        
        //방법2
		for(int i=90;i>=65;i--) {
			for(int j=65; j<i; j++) {
				System.out.print((char)j);
			}
			System.out.println();

	}

}
}

 

 

 

char 타입으로 변환을 해줘야지 숫자가 아닌 알파벳으로 추출이 된다.

 


  • Exam02_05
package sist;

/*
 * [문제5] 1 - 2 + 3 - 4 + 5 - 6 ........ - 100
 * 		    결과값을 화면에 보여주세요.
 */


public class Exam02_05 {

	public static void main(String[] args) {
		
		int sum = 0; //합을 누적시킬 변수
		
		for(int i=1; i<= 100; i++) {
			if(i%2 == 1) {
				sum = sum + i;
			}else {
				sum = sum - i;
			}
		}
		System.out.println("sum >>> " + sum);
	}

}

  • Exam02_06

 

package sist;

import java.util.Scanner;

public class Exam02_06 {

	public static void main(String[] args) {
		
			
				Scanner sc = new Scanner(System.in);
				
				System.out.println("** coffee 메뉴 ***");
				System.out.println("1. 아메리카노 - 3,000원");
				System.out.println("2. 카페라떼 - 4,000원");
				System.out.println("3. 마키아또 - 4,500원");
				System.out.println("4. 바닐라라떼 -  4,500원");
				System.out.println("위 메뉴 중 하나를 선택하세요. : ");
				
				int menuNo = sc.nextInt();
				
				System.out.println("주문 수량 : ");
				int amount = sc.nextInt();
				
				System.out.println("입금액 : ");
				int money = sc.nextInt();
						
				System.out.println();
				
				String coffeeStr = null; //커피 종류가 저장될 문자열 변수
				int price = 0;
				
				switch(menuNo) {
				case 1 :
					coffeeStr = "아메리카노";
					price = 3000;
					break;
				case 2 :
					coffeeStr = "카페라떼";
					price = 4000;
					break;
				case 3 :
					coffeeStr = "마끼아또";
					price = 4500;
					break;
				case 4 :
					coffeeStr = "바닐라라떼";
					price = 4500;
					break;
				default :
					System.out.println("선택하신 메뉴는 없는 메뉴입니다. ");
					
				}

				// 공급가액 계산 - 단가 * 수량
				int sum = price * amount;
				
				// 부가세액 계산 - 공급가액 * 0.1
				int vat = (int)(sum * 0.1);
				
				// 총금액 계산 - 공급가액 + 부가세액
				int total = sum + vat;
				
				// 잔액(거스름돈) 계산 - 입금액 - 총금액
				int change = money - total;
				
				// 계산된 결과를 화면에 보여주자.
				System.out.println("주문환 메뉴 : " + coffeeStr);
				System.out.printf("커피 단가 : %,d원\n", price);
				System.out.println("주문 수량 : " + amount);		
				System.out.printf("입  금  액 : %,d원\n", money);
				System.out.printf("공급가액 : %,d원\n", sum);
				System.out.printf("부가세액 : %,d원\n", vat);
				System.out.printf("총 금 액 : %,d원\n", total);
				System.out.printf("거스름돈 : %,d원\n", change );

				sc.close();

			}



	

}

  • Exam03_01
package sist;

import java.util.Scanner;

/*
 * [문제1] 배열의 크기를 키보드로 입력을 받고
 *        입력받은 배열의 크기만큼 키보드로 정수를
 *        입력받아서 최대값과 최소값을 화면에 출력해 보세요. 
 */

public class Exam03_01 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("배열의 크기를 입력하세요. : ");
		
		int[] arr = new int[sc.nextInt()];
		
		// 최대값 변수, 최소값 변수
		int max = 0, min = 99;
		
		// 배열에 키보드를 이용하여 임의의 정수를 입력을 받자.
		for(int i=0; i<arr.length; i++) {
			System.out.print((i+1) + "번째 정수 입력 >>> ");
			arr[i] = sc.nextInt();
			
			// 최대값을 구해 보자.
			if(arr[i] > max) {
				max = arr[i];
			}
			
			// 최소값을 구해 보자.
			if(arr[i] < min) {
				min = arr[i];
			}
		}
		
		System.out.println("최대값 >>> " + max);
		System.out.println("최소값 >>> " + min);
		
		sc.close();

	}

}

최댓값 변수 => 가장 작은 값, 최솟값 변수 => 가장 큰 값


  • Exam03_02
package sist;

/*
 * [문제2] 임의의 숫자 5개를 키보드로 입력을 받아서
 *        정수 배열에 저장 후 내림차순으로 정렬하여
 *        화면에 출력해 보세요. 
 */

import java.util.Scanner;

public class Exam03_02 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수형 배열의 크기 입력 : ");
		
		int[] score = new int[sc.nextInt()];
		
		for(int i=0; i<score.length; i++) {
			
			System.out.print((i+1) + " 번째 정수 입력 >>> ");
			
			score[i] = sc.nextInt();
		}
		
		// 내림차순으로 정렬을 진행해 보자.
		int temp = 0;
		
		for(int i=0; i<score.length; i++) {
			for(int j=i+1; j<score.length; j++) {
				if(score[j] > score[i]) {
					temp = score[i];
					score[i] = score[j];
					score[j] = temp;
				}
			}
		}
		
		
		// 내림차순으로 정렬된 배열을 화면에 출력해 보자.
		for(int i=0; i<score.length; i++) {
			System.out.print(score[i] + "\t");
		}
		System.out.println();
		
		sc.close();

	}

}

  • Exam03_03 어렵 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
package sist;

import java.util.Scanner;

/*
 * [문제] 키보드로 학생 수와 이름, 국어점수, 영어점수,
 *       수학점수를 배열에 저장한 후에 총점, 평균,
 *       학점, 석차까지 구하여 각각의 배열에 저장 후
 *       화면에 성적이 출력되도록 하세요.
 */

public class Exam03_03 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("학생 수를 입력하세요. : ");
		
		// 1. 학생 이름, 국어점수, 영어점수, 수학점수,
		//    총점, 평균, 학점, 순위 배열이 필요함.
		// int studentNo = sc.nextInt();
		String[] name = new String[sc.nextInt()];
		int[] kor = new int[name.length];
		int[] eng = new int[kor.length];
		int[] mat = new int[eng.length];
		int[] sum = new int[mat.length];
		double[] avg = new double[sum.length];
		String[] grade = new String[avg.length];
		int[] rank = new int[grade.length];
		
		// 2. 학생 수 만큼 이름, 국어점수, 영어점수, 수학점수를
		//    키보드로 입력을 받아서 각각의 배열에 저장을 해 주자.
		for(int i=0; i<name.length; i++) {
			
			/////// 이름과 각 과목의 점수를 배열에 저장 ///////
			System.out.print("이름을 입력하세요. : ");
			name[i] = sc.next();
			
			System.out.print("국어 점수 입력 : ");
			kor[i] = sc.nextInt();
			
			System.out.print("영어 점수 입력 : ");
			eng[i] = sc.nextInt();
			
			System.out.print("수학 점수 입력 : ");
			mat[i] = sc.nextInt();
			
			/////// 총점과 평균 그리고 학점을 구해 주자 ///////
			// 총점을 구하자.
			sum[i] = kor[i] + eng[i] + mat[i];
			
			// 평균을 구하자.
			avg[i] = sum[i] / 3.0;
			
			// 학점을 구하자.
			if(avg[i] >= 90) {
				grade[i] = "A학점";
			}else if(avg[i] >= 80) {
				grade[i] = "B학점";
			}else if(avg[i] >= 70) {
				grade[i] = "C학점";
			}else if(avg[i] >= 60) {
				grade[i] = "D학점";
			}else {
				grade[i] = "F학점";
			}
			
			// 석차를 구하자.
			// 모든 학생은 본인이 1등이라고 생각한다.
			rank[i] = 1;
			
		}
		
		// 실제로 석차를 구해 보자.
		for(int i=0; i<name.length; i++) {
			for(int j=0; j<name.length; j++) {
				if(sum[j] > sum[i]) {
					rank[i]++;     // 내 등수가 1++
				}
			}
		}
		
		// 마지막으로 성적을 화면에 출력해 보자.
		for(int i=0; i<name.length; i++) {
			System.out.println("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
			System.out.print("이  름 : " + name[i] + "\t");
			System.out.print("총  점 : " + sum[i] + "점\t");
			System.out.printf("평  균 : %.2f점\t", avg[i]);
			System.out.print("학  점 : " + grade[i] + "\t");
			System.out.print("석  차 : " + rank[i] + "등");
			System.out.println();
		}
		
		sc.close();

	}

}

Exam03_04

package sist;

/*
 * [문제]
 * 
 *    1   2   3   4   5
 *    6   7   8   9  10
 *   11  12  13  14  15
 *   16  17  18  19  20
 *   21  22  23  24  25
 *   
 *   - 2차원배열(5행5열)
 */

public class Exam03_04 {

	public static void main(String[] args) {
		
		// 1. 2차원배열 선언 및 메모리 할당
		int[][] arr = new int[5][5];  // 5행5열 2차원 배열
		int count = 1;
		
		// 2. 5행5열 다차원 배열에 데이터를 저장해 보자.
		for(int i=0; i<arr.length; i++) {
			for(int j=0; j<arr[i].length; j++) {
				arr[i][j] = count++;
			}
		}
		
		// 3. 다차원 배열을 화면에 출력해 보자.
		for(int i=0; i<arr.length; i++) {
			for(int j=0; j<arr[i].length; j++) {
				System.out.printf("%2d\t", arr[i][j]);
			}
			System.out.println();
		}

	}

}

  • Exam03_05 

*******정보처리기사에서 자주 나오는

package sist;

public class Exam03_05 {

	public static void main(String[] args) {
	
		int[][] arr = new int[5][5];
		int count = 1;
		
		// 1. 5행5열의 다차원배열에 데이터를 저장해 보자.
		for(int i=0; i<arr.length; i++) {     		//열
			for(int j=0; j<arr[i].length; j++) {	//행
				arr[j][i] = count++;
				
			}
		}
		
		for(int i =0; i<arr.length; i++) {
			for(int j=0; j<arr[i].length; j++) {
				System.out.printf("%2d\t", arr[i][j]);
				
			}
			System.out.println();
		}
		
	}

}

행 고정 →
열고정 ↓

행 고정 →  :  arr[ i ][ j ]

열 고정 ↓  :  arr[ j ][ i ]

 

 


  • Exam03_06

체크한 부분은 의미가 없는 공간

package sist;

/*
 * [문제]
 * 
 * 1
 * 2	3
 * 4	5	6
 * 	7	8	9	10
 * 11	12	13	14	15
 * 
 * 
 * -가변 배열을 이용하여 처리하자.
 * 
 * 
 */

public class Exam03_06 {

	public static void main(String[] args) {
		
		int[][] arr = new int[5][];
		int count = 1;
		
		//가변 배열의 열을 생성해 주자.
		//arr[0] = new int[1];
		//arr[1] = new int[2];
		//arr[2] = new int[3];
		//arr[3] = new int[4];
		//arr[4] = new int[5];
		
		for(int i=0; i<arr.length; i++) {
			arr[i] = new int[i+1];
		}
		
		// 2. 가변 배열에 데이터를 저장해 주자.
		for(int i=0; i<arr.length;i++) {
			for(int j=0; j<arr[i].length; j++) {
				arr[i][j] = count++;
			}
		}
		
		// 3.가변 배열에 저장된 데이터를 화면에 출력해 보자.
		for(int i=0; i<arr.length; i++) {
			for(int j=0; j<arr[i].length; j++) {
				System.out.printf("%2d\t", arr[i][j]);
				
			}
			System.out.println();
		}
		
		
		
	}

}

Method                                                              

작업을 하는 놈

( ) 괄호 열고 닫고가 되어 있으면 메서드이다.