본문 바로가기
Java/└ [JAVA] Practice

[실습문제] 반복문, 분기문

by 박채니 2022. 4. 11.

안녕하세요, 코린이의 코딩 학습기 채니 입니다.

 

반복문과 분기문에 대해서 간단한 실습 문제들을 풀어보았습니다.

 

개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.


@실습 문제1

3명의 회원정보를 입력 받아 저장하고 출력하는 
프로그램을 만드세요
 입력 : 이름, 나이, 주소, 키, 몸무게, 연락처

출력 예) 
================ 저장회원===============
1 홍길동 19세 서울시 강남구 170cm 68kg 01012345678
2 임걱정 20세 경기도 수원시 180cm 75kg 01012345678
                           
기능추가1
출력결과 마지막행에 다음 항목에 대한 평균을 구하시오.
============================================
평균나이 00세 / 평균 키 : 00CM / 평균 몸무게 : 00kg

기능추가2
입력할 회원 수를 입력하고 그 수 만큼 회원을 입력 받게 업그레이드 하세요

 

public class Test5 {
	public static void main(String[] args) {
		Test5 te = new Test5();
//		te.test1();
//		te.test2();
		te.test3();
	}
	
	public void test3() {
		Scanner sc = new Scanner(System.in);

		String name = "";
		int age = 0;
		String address = "";
		int height = 0;
		int weight = 0;
		String phone = "";
		
		String result = "======================== 저장회원=======================\n";
		int count = 3;
		
		int addAge = 0;
		int addHeight = 0;
		int addWeight = 0;
		
		double avgAge = 0.0;
		double avgHeight = 0.0;
		double avgWeight = 0.0;
		
		System.out.print("> 몇 명의 회원을 입력하시겠습니까? : ");
		int countMem = sc.nextInt();
		
		for(int i = 0; i < countMem; i++) {
			System.out.print("> 이름을 입력하세요 : ");
			name = sc.next();
			System.out.print("> 나이를 입력하세요 : ");
			age = sc.nextInt();
			System.out.print("> 주소를 입력하세요 : ");
			sc.nextLine();
			address = sc.nextLine();
			System.out.print("> 키와 몸무게를 입력하세요 : ");
			height = sc.nextInt();
			weight = sc.nextInt();
			System.out.print("> 핸드폰 번호를 입력하세요 : ");
			phone = sc.next();
			
			addAge += age;
			addHeight += height;
			addWeight += weight;
			
			result += (i+1) + " "+ name + "\t" + age + "\t" + address + "\t" + height + "cm\t" + weight + "kg\t" + phone + "\n";
		}
		result += "======================================================\n";
		
		avgAge = age/countMem;
		avgHeight = height/countMem;
		avgWeight = weight/countMem;
		
		result += "평균나이 " + avgAge + " / 평균 키 : " + avgHeight + "cm / 평균 몸무게 : " + avgWeight + "kg";  
		System.out.println(result);
	}

	public void test2() {
		Scanner sc = new Scanner(System.in);
		String name = "";
		int age = 0;
		String address = "";
		int height = 0;
		int weight = 0;
		String phone = "";
		
		String result = "======================== 저장회원=======================\n";
		int count = 3;
		
		int addAge = 0;
		int addHeight = 0;
		int addWeight = 0;
		double avgAge = 0.0;
		double avgHeight = 0.0;
		double avgWeight = 0.0;
		
		for(int i = 0; i < count; i++) {
			System.out.print("> 이름을 입력하세요 : ");
			name = sc.next();
			System.out.print("> 나이를 입력하세요 : ");
			age = sc.nextInt();
			System.out.print("> 주소를 입력하세요 : ");
			sc.nextLine();
			address = sc.nextLine();
			System.out.print("> 키와 몸무게를 입력하세요 : ");
			height = sc.nextInt();
			weight = sc.nextInt();
			System.out.print("> 핸드폰 번호를 입력하세요 : ");
			phone = sc.next();
			
			addAge += age;
			addHeight += height;
			addWeight += weight;
			
			result += (i+1) + " " + name + "\t" + age + "\t" + address + "\t" + height + "cm\t" + weight + "kg\t" + phone + "\n";
		}
		result += "======================================================\n";
		
		avgAge = addAge / count;
		avgHeight = addHeight / count;
		avgWeight = addWeight / count;
		
		result += "평균나이 " + avgAge + " / 평균 키 : " + avgHeight + "cm / 평균 몸무게 : " + avgWeight + "kg";
		
		System.out.println(result);
			
	}
	
	public void test1() {
		Scanner sc = new Scanner(System.in);
		String name = "";
		int age = 0;
		String address = "";
		int height = 0;
		int weight = 0;
		String phone = "";
		
		String result = "======================== 저장회원=======================\n";
		int count = 3;
		
		for(int i = 0; i < count; i++) {
			System.out.print("> 이름을 입력하세요 : ");
			name = sc.next();
			System.out.print("> 나이를 입력하세요 : ");
			age = sc.nextInt();
			System.out.print("> 주소를 입력하세요 : ");
			sc.nextLine();
			address = sc.nextLine();
			System.out.print("> 키와 몸무게를 입력하세요 : ");
			height = sc.nextInt();
			weight = sc.nextInt();
			System.out.print("> 핸드폰 번호를 입력하세요 : ");
			phone = sc.next();
			
			result += (i+1) + " " + name + "\t" + age + "\t" + address + "\t" + height + "\t" + weight + "\t" + phone + "\n";
		}
		result += "======================================================\n";
		System.out.println(result);
	}
}

 

 

@실습 문제2

분식집 주문 받는 프로그램을 만들어 보세요.
    
    1. 메뉴선택
    2. 수량선택
    3. 추가주문선택 
    4. y면 1번부터 다시 시작. 단 데이터는 누적 
    5. n면 주문결과서 출력 

    메뉴
    김밥류 ===================
    1.원조김밥===========1500원
    2.치즈김밥===========2000원
    3.참치김밥===========2300원
    라면류 ===================
    4.그냥라면===========3000원
    5.치즈라면===========3500원
    6.짬뽕라면===========4000원
    분식류 ===================
    7.떡볶이=============2500원
    8.순대==============2500원
    9.오뎅==============1000원
    기타 ====================
    10.음료수===========1000원

    결과
    주문하신 정보는 다음과 같습니다.
    ----------------------------
    치즈라면: 3개 - 10500원
    그냥라면: 3개 - 9000원
    음료수: 3개 - 3000원
    ----------------------------
    합계 : 22500원

 

Scanner sc = new Scanner(System.in);
		
String menu = "김밥류 ===================\n"
		+ "1.원조김밥===========1500원\n"
		+ "2.치즈김밥===========2000원\n"
		+ "3.참치김밥===========2300원\n"
		+ "라면류 ===================\n"
		+ "4.그냥라면===========3000원\n"
		+ "5.치즈라면===========3500원\n"
		+ "6.짬뽕라면===========4000원\n"
		+ "분식류 ===================\n"
		+ "7.떡볶이=============2500원\n"
		+ "8.순대==============2500원\n"
		+ "9.오뎅==============1000원\n"
		+ "기타 ====================\n"
		+ "10.음료수===========1000원\n";
		
String order = "";
int price = 0;
int count = 0;
int sum = 0;
		
while(true) {
	System.out.println(menu);
	System.out.print("> 메뉴 선택 : ");
	String menuChoice = sc.next();
	System.out.print("> 수량 선택 : ");
	count = sc.nextInt();
			
	switch(menuChoice) {
	case "1" : 
		price = 1500*count;
		sum+=price;
		order += "원조김밥 : " + count + "개 - " + price + "원\n";
		break;
	case "2" : 
		price = 2000*count;
		sum+=price;
		order +="치즈김밥 : " + count + "개 - " + price + "원\n";
		break;
	case "3" :
		price = 2300*count;
		sum+=price;
		order += "참치김밥 : " + count + "개 - " + price + "원\n";
		break;
	case "4" :
		price = 3000*count;
		sum+=price;
		order += "그냥라면 : " + count + "개 - " + price + "원\n";
		break;
	case "5" :
		price = 3500*count;
		sum+=price;
		order += "치즈라면 : " + count + "개 - " + price + "원\n";
		break;
	case "6" :
		price = 4000*count;
		sum+=price;
		order += "짬뽕라면 : " + count + "개 - " + price + "원\n";
		break;
	case "7" : 
		price = 2500*count;
		sum+=price;
		order += "떡볶이 : " + count + "개 - " + price + "원\n";
		break;
	case "8" :
		price = 2500*count;
		sum+=price;
		order += "순대 : " + count + "개 - " + price + "원\n";
		break;
	case "9" : 
		price = 1000*count;
		sum+=price;
		order += "오뎅 : " + count + "개 - " + price + "원\n";
		break;
	case "10" :
		price = 1000*count;
		sum+=price;
		order += "음료수 : " + count + "개 - " + price + "원\n";
		break;
	default : System.out.println("잘못 입력하셨습니다."); break;
	}
			
	System.out.print("추가 주문 하시겠습니까?(y/n) : ");
	char yn = sc.next().charAt(0);
	if(yn == 'n') {
		System.out.print("주문하신 정보는 다음과 같습니다.\n"
				+ "----------------------------\n"
				+ order
				+ "----------------------------\n"
				+ "합계 : " + sum);
		break;
	}
}

@콘솔출력값
김밥류 ===================
1.원조김밥===========1500원
2.치즈김밥===========2000원
3.참치김밥===========2300원
라면류 ===================
4.그냥라면===========3000원
5.치즈라면===========3500원
6.짬뽕라면===========4000원
분식류 ===================
7.떡볶이=============2500원
8.순대==============2500원
9.오뎅==============1000원
기타 ====================
10.음료수===========1000원

> 메뉴 선택 : 4
> 수량 선택 : 3
추가 주문 하시겠습니까?(y/n) : y
김밥류 ===================
1.원조김밥===========1500원
2.치즈김밥===========2000원
3.참치김밥===========2300원
라면류 ===================
4.그냥라면===========3000원
5.치즈라면===========3500원
6.짬뽕라면===========4000원
분식류 ===================
7.떡볶이=============2500원
8.순대==============2500원
9.오뎅==============1000원
기타 ====================
10.음료수===========1000원

> 메뉴 선택 : 1
> 수량 선택 : 3
추가 주문 하시겠습니까?(y/n) : n
주문하신 정보는 다음과 같습니다.
----------------------------
그냥라면 : 3개 - 9000원
원조김밥 : 3개 - 4500원
----------------------------
합계 : 13500

 

 

@실습문제3

- 메소드명 : public void test1()
    1) 정수 한 개를 입력 받아, 1부터 입력 받은 정수까지의 홀수의 곱을 출력하세요.

- 메소드명 : public void test2()
    2) 사용자로부터 정수를 두개 입력 받아서, 작은 정수에서 큰 정수까지 홀수의 합을 구하세요.
         Tip. 큰수/작은수에 해당하는 변수를 사용할 것.
     
         결과:
         정수를 하나 입력하세요. => 100
         다른 정수를 하나 입력하세요. => 1
         ====================
         1부터 100까지의 홀수의 합은 2500입니다.

 

public void test1() {
	System.out.print("> 정수 하나를 입력하세요 : ");
	int input = sc.nextInt();
		
	int num = 1;
	for(int i = 1; i <= input; i++) {
		if(i % 2 == 0) continue;
		num*=i;
	}
	System.out.println(num);
}

@콘솔출력값
> 정수 하나를 입력하세요 : 10
945

public void test2() {
		System.out.print("> 정수 1 입력하세요 : ");
		int firstNum = sc.nextInt();
		System.out.print("> 정수 2 입력하세요 : ");
		int secNum = sc.nextInt();
		
		int big = 0, small = 0;
		int sum = 0;
		
		if(firstNum > secNum) {
			big = firstNum;
			small = secNum;
		}
		else if(firstNum < secNum) { 
			big = secNum;
			small = firstNum;
		} else if(secNum == firstNum) {
			big = firstNum;
			small = secNum;
		}
		
		for(int i = small; i <= big; i++) {
			if(i % 2 == 0)
				continue;
			sum+=i;
		}
		System.out.printf("%d부터 %d까지의 홀수의 합은 %d입니다.\n", small, big, sum);
	}
    
@콘솔출력값
> 정수 1 입력하세요 : 100
> 정수 2 입력하세요 : 1
1부터 100까지의 홀수의 합은 2500입니다.

 

'Java > └ [JAVA] Practice' 카테고리의 다른 글

[실습문제] 중첩 반복문, 2차원 배열  (0) 2022.04.13
[실습문제] 배열  (0) 2022.04.13
[실습문제] switch, for문  (0) 2022.03.15
[실습문제] 난수, 조건문  (0) 2022.03.10
[실습문제] 연산자  (0) 2022.03.10