일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 26 |
27 | 28 | 29 | 30 |
Tags
- 대덕인재개발원
- FastAPI
- 자바문제
- 컬렉션프레임워크
- servlet
- ibatis
- Oracle
- Error
- 객체지향
- 반복문
- Mac
- 생활코딩
- Java
- API
- 맥
- html
- python
- ddit
- 배열
- 단축키
- crud
- spring
- nodejs
- 자바
- JDBC
- pyqt
- 이클립스
- Homebrew
- jsp
- Android
Archives
- Today
- Total
romworld
Chapter 04. 조건문과 반복문 문제 (삼각형 판별,월 입력, 점수평균, 가위바위보게임,거스름돈, 랜덤맞추기,베이스볼,로또) 본문
JAVA/문제풀이
Chapter 04. 조건문과 반복문 문제 (삼각형 판별,월 입력, 점수평균, 가위바위보게임,거스름돈, 랜덤맞추기,베이스볼,로또)
inderrom 2022. 9. 13. 01:14문제 1
: 삼각형 판별
Scanner sc = new Scanner(System.in);
System.out.println("삼각형의 첫번째 변의 길이를 입력하세요: ");
int a = Integer.parseInt(sc.nextLine());
System.out.println("삼각형의 두번째 변의 길이를 입력하세요: ");
int b = Integer.parseInt(sc.nextLine());
System.out.println("삼각형의 세번째 변의 길이를 입력하세요: ");
int c = Integer.parseInt(sc.nextLine());
if((a + b) > c && (a + c) > b && b + c > a) {
System.out.println("삼각형을 만들 수 있습니다.");
}else {
System.out.println("삼각형을 만들 수 없습니다.");
}
문제 2
:월 입력
Scanner sc = new Scanner(System.in);
outer:
do {
System.out.println("월 입력 >>");
int input = Integer.parseInt(sc.nextLine());
switch(input) {
case 3: case 4: case 5:
System.out.println("봄");
break;
case 6: case 7: case 8:
System.out.println("여름");
case 9: case 10: case 11:
System.out.println("가을");
break;
case 12: case 1: case 2:
System.out.println("겨울");
break;
default:
System.out.println("잘못입력");
break outer;
}
}while(true);
do while은 최소 한번은 꼭 실행된당!
문제 3
: 점수 입력받고 평균 구하기
package sec03.exam01;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("점수를 입력하세요.");
System.out.println("프로그래밍 기초: ");
int score1 = Integer.parseInt(sc.nextLine());
System.out.println("데이터베이스: ");
int score2 = Integer.parseInt(sc.nextLine());
System.out.println("화면 구현: ");
int score3 = Integer.parseInt(sc.nextLine());
System.out.println("애플리케이션 구현: ");
int score4 = Integer.parseInt(sc.nextLine());
System.out.println("머신러닝: ");
int score5 = Integer.parseInt(sc.nextLine());
int sum = score1 + score2 + score3 + score4 + score5;
System.out.println("총점: " + sum);
double avg = sum / 5.00;
System.out.printf("평균: %.2f", avg);
System.out.println();
String grade = "";
if(avg >= 90) {
grade = "A";
}else if (avg >= 80) {
grade = "B";
}else if (avg >= 70) {
grade = "C";
}else if (avg >= 60) {
grade = "D";
}else {
grade = "F";
}
System.out.println("학점: " + grade);
}
}
문제 4
:가위바위보 게임
package sec03.exam01;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("가위 바위 보 게임");
System.out.println("철수: ");
String Chulsu = sc.nextLine();
System.out.println("영희: ");
String Younghui = sc.nextLine();
switch(Chulsu) {
case "가위" :
if(Younghui.equals("가위")) {
System.out.println("결과: 무승부");
break;
}else if (Younghui.equals("바위")) {
System.out.println("결과: 영희 승리!");
break;
}else {
System.out.println("결과: 철수 승리!");
break;
}
case "바위" :
if(Younghui.equals("가위")) {
System.out.println("결과: 철수 승리!");
break;
}else if (Younghui.equals("바위")) {
System.out.println("결과: 무승부");
break;
}else {
System.out.println("결과: 영희 승리!");
break;
}
case "보" :
if(Younghui.equals("가위")) {
System.out.println("결과: 영희 승리!");
break;
}else if (Younghui.equals("바위")) {
System.out.println("결과: 철수 승리!");
break;
}else {
System.out.println("결과: 무승부");
break;
}
default : System.out.println();
break;
}
}
}
문제 5
: 거스름돈
import java.util.Random;
public class hello {
public static void main(String[] args) {
Random rnd = new Random();
int money = (rnd.nextInt(500) +1) * 10;
System.out.println("거스름돈 : " + money);
int coin500 = money / 500;
money %= 500;
int coin100 = money / 100;
money %= 100;
int coin50 = money / 50;
money %= 50;
int coin10 = money / 10;
System.out.printf("500원 : %d개\n" , coin500);
System.out.printf("100원 : %d개\n" , coin100);
System.out.printf("50원 : %d개\n" , coin50);
System.out.printf("10원 : %d개\n" , coin10);
}
}
문제 6
: 랜덤 맞추기
package sec03.exam01;
import java.util.Random;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Random rnd = new Random();
Scanner sc = new Scanner(System.in);
int num1 = rnd.nextInt(100) + 1;
int num2;
int count = 0;
do {
System.out.println("1부터 100 사이의 정수 중 하나를 선택하세요: ");
num2 = sc.nextInt();
if(num2 < num1) {
System.out.println("정답은 더 큰 수입니다.");
}else if (num2 > num1) {
System.out.println("정답은 더 작은 수입니다.");
}else if(num1 == num2) {
}count++;
}while(num1 != num2);
System.out.println("정답입니다." + count + "번 만에 맞추셨습니다\n게임을 종료합니다.");
sc.close();
}
}
문제 7
:베이스볼 게임
* 세 자리 랜덤한 숫자를 맞추는 게임을 만들어보자.
* 세 자리 숫자는 다음과 같은 규칙을 갖는다.
* 1. 중복되는 숫자가 없음(예: 133 X, 333 X, ...)
* 2. 0이 없다. (예: 130 X, 907 X, 012 X, ...)
* 랜덤으로 만들어진 숫자를 저장해 두고
* 사용자에게 세 자리 숫자를 입력받아 다음과 같이 표기한다.
* 1. 같은 숫자이면서 위치가 같을경우 S
* 2. 같은 숫자는 존재하지만 위차가 다를 경우 B
* 3. 같은 숫자가 없는 경우 O
* 맞는 숫자를 입력때까지 계속한다.
* 출력 예시 :
* ...
* 세 자리 숫자 입력 >> 571
* 6차 시도(571) : 2S 0B 1O
* --------------------------
* 세 자리 숫자 입력 >> 671
* 7차 시도(671) : 3S 0B 0O
* ---------------------------
* 정답입니다!!
Random rnd = new Random();
int num1, num2, num3;
num1 = rnd.nextInt(9) + 1; // 0~8 + 1 => 1~9
do {
num2 = rnd.nextInt(9) + 1;
}while(num1 == num2);
do {
num3 = rnd.nextInt(9) + 1;
}while(num1 == num3 || num2 == num3);
System.out.println("정답 : " + num1 + num2 + num3);
Scanner sc = new Scanner(System.in);
int count = 0;
while(true) {
count++;
System.out.print("규칙에 맞는 숫자 입력 >>");
int input = Integer.parseInt(sc.nextLine());
int s = 0, b= 0, o = 0;
//방법1
//369 / 100 =
// int i1 = input / 100;
// input %= 100;
// int i2 = input / 10;
// input %= 10;
// int i3 = input;
//
//369
//방법2
// 369 % 10 => 9
int i3 = input % 10;
//369 / 10 => 36
input /= 10;
// 36 % 10 => 6
int i2 = input % 10;
//36 / 10 => 3
input /= 10;
int i1 = input;
if(num1 == i1) s++;
if(num2 == i2) s++;
if(num3 == i3) s++;
if(i1 == num2 || i1 == num3) b++;
if(i2 == num1 || i2 == num3) b++;
if(i3 == num1 || i3 == num2) b++;
o = 3 - (s + b);
System.out.printf(count +"차 시도(%d%d%d) : %dS %dB %dO\n", i1, i2, i3, s, b, o);
System.out.println("----------------");
if(s == 3) {
System.out.println("정답입니다!!");
break;
}
}
문제 8
:로또 문제 !
임의의 로또 번호 6자리를 생성하고 < 1등번호
(중복번호 없음, 1~45)
1등이 나올때까지 랜덤 6자리 생성하여
얼마를 써야 1등이 되는지 알려주세요.
package c_statement;
import java.util.Random;
import java.util.Scanner;
public class Z_Lotto {
public static void main(String[] args) {
Random lotto = new Random();
int num1, num2, num3, num4, num5, num6;
num1 = lotto.nextInt(45) + 1;
do {
num2 = lotto.nextInt(45) + 1;
} while (num2 == num1);
do {
num3 = lotto.nextInt(45) + 1;
} while (num3 == num1 || num3 == num2);
do {
num4 = lotto.nextInt(45) + 1;
} while (num4 == num1 || num4 == num2 || num4 == num3);
do {
num5 = lotto.nextInt(45) + 1;
} while (num5 == num1 || num5 == num2 || num5 == num3 || num5 == num4);
do {
num6 = lotto.nextInt(45) + 1;
} while (num6 == num1 || num6 == num2 || num6 == num3 || num6 == num4 || num6 == num5);
Scanner sc = new Scanner(System.in);
System.out.printf("====로또 1등 당첨번호======\n %d : %d : %d : %d : %d : %d\n", num1, num2, num3, num4, num5, num6);
long count = 0;
while (true) {
int a = 0;
int n1, n2, n3, n4, n5, n6;
//계속 틀리는 내 로또번호
n1 = lotto.nextInt(45) + 1;
do {
n2 = lotto.nextInt(45) + 1;
} while (n2 == n1);
do {
n3 = lotto.nextInt(45) + 1;
} while (n3 == n1 || n3 == n2);
do {
n4 = lotto.nextInt(45) + 1;
} while (n4 == n1 || n4 == n2 || n4 == n3);
do {
n5 = lotto.nextInt(45) + 1;
} while (n5 == n1 || n5 == n2 || n5 == n3 || n5 == n4);
do {
n6 = lotto.nextInt(45) + 1;
} while (n6 == n1 || n6 == n2 || n6 == n3 || n6 == n4 || n6 == n5);
count++;
//false false true
if (num1 == n1 || num1 == n2 || num1 == n3 || num1 == n4 || num1 == n5 || num1 == n6) a++;
if (num2 == n1 || num2 == n2 || num2 == n3 || num2 == n4 || num2 == n5 || num2 == n6) a++;
if (num3 == n1 || num3 == n2 || num3 == n3 || num3 == n4 || num3 == n5 || num3 == n6) a++;
if (num4 == n1 || num4 == n2 || num4 == n3 || num4 == n4 || num4 == n5 || num4 == n6) a++;
if (num5 == n1 || num5 == n2 || num5 == n3 || num5 == n4 || num5 == n5 || num5 == n6) a++;
if (num6 == n1 || num6 == n2 || num6 == n3 || num6 == n4 || num6 == n5 || num6 == n6) a++;
if(a == 6) {
System.out.println(count + "번 긁었습니다.");
break;
}
}
System.out.println("금액은 " + count * 1000 + " 입니다.");
}
}
'JAVA > 문제풀이' 카테고리의 다른 글
chapter 05-4. 다차원배열 문제 (학생들의 평균 구하기) (1) | 2022.09.23 |
---|---|
Chapter 05-2. 배열 문제 (거스름돈, *그래프, 중복값 제거,로또번호, 가위바위보, 주사위 출력) (0) | 2022.09.18 |
Chapter 04. 조건문과 반복문 문제 ( Counting Star) (1) | 2022.09.14 |
Chapter 03. 연산자 문제 (홀수짝수출력, 몫과나머지,메트로폴리스,윤년) (0) | 2022.09.11 |
Chapter 02. 변수와 타입 문제 (직사각형, 프록시마 센타우리, 원기둥, 저금통)-입출력 (0) | 2022.09.10 |
Comments