import java.util.Scanner; //Scanner 클래스 사용하기 위한 import 작업
public class EscapeRoom {//class 파일명 EscapeRoom
public static void main(String[] args) {
//0.준비
Scanner scanner = new Scanner(System.in); //사용자에게 입력을 받기 위한 객체 생성
int count = 0; // 3문제를 맞춘 것을 알기 위해 0을 초기값으로 잡는다.
while (count < 3){ //맞춘 문제 개수가 3개 미만일 때
//1.입력
System.out.print("Input 2 numbers: "); //출력
int x = scanner.nextInt(); //입력 받은 값 하나를 변수 x로 설정한다.
int y = scanner.nextInt(); //입력 받은 값 하나를 변수 y로 설정한다.
System.out.println(x + "+" + y + " = ?");/출력
int num = scanner.nextInt(); //출력에 대한 입력 받은 값을 num으로 저장한다.
//2.처리
boolean result = num == x + y; //num과 x+y가 같고 그 값을 boolean 데이터타입 result 변수에 할당한다.
if (result) {
count += 1;//reuslt가 참이라면 count에 1을 더한다.
}
//3.출력
if (result) {
System.out.println("Your're right!" + "(" + count + ")");
}
if (!result) { result가 거짓이라면 Your wrong을 출력한다.
System.out.println("Your wrong");
}
}
System.out.println("Escape!");//while문이 끝나면 Escape를 출력하고 종료한다.
}
}
ㅁ곱하기 정렬.
public class GuguClass {
public static void main(String[] args) {
for (int i = 2; i < 10; i += 3) {//반복문을 사용.초기값2,종료조건은 i<10일 때,i값에 3을 더 하면서 반복한다.
for (int j =1; j < 10; j+=1) {
System.out.print(i + " * " + j + " = " + (i * j));
System.out.print("\t");
System.out.print((i+1) + " * " + j + " = " + ((i+1) * j));
System.out.print("\t");
if(i+2==10){
System.out.println(); //만약 System.out.println()이 없다면 continue가 작동해
i+2==10인동안 밑에 부분이 출력 안된다.
continue;
}
System.out.println((i+2) + " * " + j + " = " + ((i+2) * j));
}
System.out.println();
}
}
}
가드크로즈 기법: 우리가 실행하려는 것이 아닌 것을 미리 나타내는 것.
for (int i = 0; i < 3; i += 1) {
for (int j = 0; j < 3; j += 1) {
if (answers[i] != numbers[j]) { // 가드크로즈 기법
continue;
}
if (i == j) {
strike += 1;
continue;
}
ball += 1;
}
}
ㅁ 번호 3개를 랜덤으로 뽑아 번호 3개를 입력해 번호와 위치 둘다 맞추면 strike, 번호 맞추고 위치 틀리면 ball
3strike가 나올 때까지 반복.
Scanner scanner = new Scanner(System.in); //
Random random = new Random();
//정답 준비
int[] numbers = new int[3];
for(int i = 0; i < 3; i +=1){
numbers[i] = random.nextInt(10);//임의의 숫자 -> 랜덤 ,끝에만 적으면 그 숫자 미만까지의 범위를 잡는다.
}
for (int i = 0; i < numbers.length; i += 1) { //6번
System.out.println(numbers[i]); // 정답을 미리보기 위해
}
int strike = 0;
int ball = 0;
//메인 루프
while (strike != 3) {
// 1. 입력
System.out.print("Guess number: ");
int[] answers = new int[3]; // 타입이라는걸 쓸 때는 기본형 타입과 string같은 것은 바로 쓸 수 있지만
// 그렇지 않은 경우 type이라는걸 쓸 때는 new로 생성을 해준다.
for(int i=0; i<3; i +=1){
answers[i] = scanner.nextInt();
}
// 2. 처리
strike = 0;
ball = 0;
for (int i = 0; i < 3; i += 1) {
for (int j = 0; j < 3; j += 1) {
if (answers[i] != numbers[j]) { // 가드크로즈 기법
continue;
}
if (i == j) {
strike += 1;
continue;
}
ball += 1;
}
}
int[] numbers = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i = 0; i < 20; i += 1) {
int x = random.nextInt(10);
int y = random.nextInt(10);
//스왑:numbers x 와 numbers y를 서로 바꾼다. -> x를 다른 곳에 보내고 y를 x에 덮어쓰고 다른 곳에 보낸걸 다시 x에 보낸다.
int temp = numbers[x];
numbers[x] = numbers[y];
numbers[y] = temp;
}
// for (int i = 0; i < 3; i += 1) {
// numbers[i] = random.nextInt(10);//임의의 숫자 -> 랜덤 ,끝에만 적으면 그 숫자 미만까지의 범위를 잡는다.
// }
for (int i = 0; i < numbers.length; i += 1) { //6번
System.out.print(numbers[i]+" "); // 정답을 미리보기 위
}
System.out.println();
int strike = 0;
int ball = 0;