package Day01;
import java.util.Random;
import java.util.Scanner;
public class Q1 {
static void main() {
//1. 난수를 만들어야 한다.
Random r = new Random(); // Random(참조 자료형) 객체를 생성 -> 변수명 r
//2. 난수를 가지고 숫자 3개를 만든다(1~9)
int com1 = 0;
int com2 = 0;
int com3 = 0;
//2-1. 각 숫자들은 중복이 나오면 안된다.
// com1, com2, com3 모두 중복이 나오면 반복
while (com1 == com2 || com2 == com3 || com1 == com3) {
com1 = r.nextInt(9) + 1;
com2 = r.nextInt(9) + 1;
com3 = r.nextInt(9) + 1;
}
// 중복 없다!!!!
System.out.println(com1 + "," + com2 + "," + com3);
//사용자 입력
// 맞출때 까지 -> 반복문
int user1;
int user2;
int user3;
boolean check = true;
Scanner sc = new Scanner(System.in); // Scanner 객체 생성
while (check) {
System.out.print("첫째 자리:");
user1 = sc.nextInt();
System.out.print("둘째 자리:");
user2 = sc.nextInt();
System.out.print("셋째 자리:");
user3 = sc.nextInt();
// 1~9 입력이 아니면
if (user1 < 1 || user1 > 9 || user2 < 1 || user2 > 9 || user3 < 1 || user3 > 9) {
System.out.println("잘못된 입력입니다.");
continue;
}
if (user1 == user2 || user2 == user3 || user1 == user3) {
System.out.println("잘못된 입력입니다.");
continue;
}
int ball = 0;
int strike = 0;
// 올바른 입력 확인
// strike ball out 홈런
// ball 나올수 있는 수 3
// strike 나올수 있는 수 2
// out -> ball, strike 둘다 0
// 홈런 -> strike 3
// strike 확인
if (user1 == com1) {
strike++;
}
if (user2 == com2) {
strike++;
}
if (user3 == com3) {
strike++;
}
// ball 확인
if (user1 == com2 || user1 == com3) {
ball++;
}
if (user2 == com1 || user2 == com3) {
ball++;
}
if (user3 == com1 || user3 == com2) {
ball++;
}
// 출력 및 종료
if (strike == 0 && ball == 0) {
System.out.println("Out 입니다.");
} else if (strike == 3) {
System.out.println("홈런 입니다.");
check = false;
} else {
System.out.println("strike : " + strike + ", ball :" + ball);
}
}
}
}
import java.util.Random;
import java.util.Scanner;
public class Q2 {
static void main() {
Random r = new Random();
int[] com = new int[3]; // 3층짜리 [0] [1] [2]
for (int i = 0; i < com.length; i++) { // 0 -> 1 -> 2
com[i] = r.nextInt(9)+1;
// i = 0 1 2
// 비교 대상 <-> 비교 값
for (int j = 0; j < i; j++) {
if (com[j] == com[i]) {
i--;
break;
}
}
}
int[] user = new int[3];
Scanner sc = new Scanner(System.in);
while (true) { // 숫자 야구 게임 홈런 나올 때까지 반복
for (int i = 0; i < user.length; i++) { // 사용자 입력 반복문
System.out.print((i+1) + "번째: "); // 문자열 출력
user[i] = sc.nextInt();// 사용자 숫자 입력
// 잘못된 입력 확인 (범위)
if (user[i] < 1 || user[i] > 9) {
System.out.println("잘못된 입력입니다.");
i--;
continue;
}
// 중복 잘못된 입력
for (int j = 0; j < i; j++) {
if (user[j] == user[i]) {
System.out.println("잘못된 입력입니다.");
i--;
break;
}
}
}
int strike = 0;
int ball = 0;
for (int i = 0; i < user.length; i++) {
for (int j = 0; j < com.length; j++) {
if (user[i] == com[j]) {
if (i == j) {
strike++;
}
else {
ball++;
}
}
}
}
if (strike == 3) {
System.out.println("홈런입니다.");
break;
}
else if (strike == 0 && ball == 0) {
System.out.println("아웃입니다.");
}
else {
System.out.println("strike: " + strike + ", ball : " + ball);
}
}
}
}
사용자의 입력은 받은 숫자를 순서대로 출력하되 3의 배수인 숫자에만 Fizz로 출력하라.
import java.util.Scanner;
public class p01 {
static void main() {
System.out.print("숫자를 입력하세요: ");
Scanner sc = new Scanner(System.in);
int user = sc.nextInt();
for (int i = 1; i <= user; i ++) {
if (i % 3 == 0) {
System.out.println("Fizz");
} else {
System.out.println(i);
}
}
}
}
사용자가 입력한 값을 순서대로 출력하되 3의 배수인 숫자만 출력하지 마라.
import java.util.Scanner;
public class p01 {
static void main() {
Scanner sc = new Scanner(System.in);
System.out.print("숫자를 입력하세요: ");
int user = sc.nextInt();
for (int i = 1; i <= user; i++) {
if (i % 3 == 0) {
continue;
} else {
System.out.print(i + " ");
}
}
}
}
오늘은 강사님이 바뀌셔서 소개와 전체적인 상담이 진행되었다.
vscode대신 intellij를 다운로드 받았고 이것저것 바뀌는 게 있었다.
그 덕에 진도는 아예 못나갔고... 선생님께서 문제를 하나 내주셨다.
간단한 야구게임이었는데 사실 솔직히 나 혼자 작성하기엔 어려움이 컸다.
java를 많이 풀어보고 공부를 더 열심히 해야겠다...