랜덤으로 1또는 7 을 10번출력한다. 7이 연속으로 3번이상이면 "당첨" 아니면 "꽝" 출력하시오.
[예시]
7 7 1 11 7 1 : 꽝
1 7 7 7 1 1 7 : 당첨
Random rnd = new Random();
int num1 = 1;
int num2 = 7;
int rNum;
int count = 0;
int combo = 0;
for (int i = 0; i < 10; i++) {
count +=1;
rNum = rnd.nextInt(2);
// 출력문
if (rNum == 0) {
count = 0;
System.out.print(num1 + " ");
} else if (rNum == 1) {
System.out.print(num2 + " ");
}
// 7 연속 판별
if (count==3) {
combo = count;
}
}
System.out.println();
if (combo == 3) {
System.err.println("당첨");
} else {
System.out.println("꽝");
}
철수는 오징어게임에 참가하였다.
첫번째 게임은 "무궁화꽃이 피었습니다" 이다.
규칙은 아래와같다.
// 승리조건 10턴 이하, 위치 25이상 / 패배조건 10턴 초과 위치 25미만, 인형 위치 이상
// 철수 매턴 1~4, 인형 3~5
Random rnd = new Random();
int shuDice, dollDice;
int shuPoint = 0;
int dollPoint = 0;
int endPoint = 25;
boolean stop = true;
System.out.println("무궁화 꽃이 피었습니다");
for (int i=0; i < 10; i++) {
System.out.println();
System.out.println( (i+1) +"번째 턴");
// 주사위 범위 철수 1~4, 인형 3~5
shuDice = rnd.nextInt(4)+1;
System.out.println("철수 "+ shuDice +" 지점 만큼 이동");
dollDice = rnd.nextInt(3)+3;
System.out.println("인형 "+ dollDice +" 체 증가");
if (shuDice > dollDice) {
System.err.println("철수 움직임 발각, 게임 종료");
stop = false;
break;
}
//각 위치들 구하기
shuPoint = shuPoint + shuDice;
System.out.println("현재 철수 위치 : " + shuPoint );
dollPoint = dollPoint + dollDice;
System.out.println("현재 인형의 수 : " + dollPoint );
//해당 턴 내에 승리조건 판별
if (shuPoint >= endPoint) {
System.out.println("철수의 승리!");
break;
}
}
//반복문 끝날때까지 철수가 골 지점에 도착했는지 판별
if (shuPoint < 25 && stop) {
System.err.println("엔드포인트까지 도착 못함. 철수의 패배");
}
철수는 요즘 주식에 관심을 보이기 시작했다.
주식의 상승곡선이 궁금하기 시작했다.
최근 관심있는 게임주식을 살펴보기로 했다.
꾸준히 상승하는 기간중 가장 긴 기간을 출력해보자.
랜덤으로 1~8까지 숫자 10개를 순차적으로 출력한다.
랜덤숫자는 주가 수치를 뜻하고 숫자가크면 상승 낮으면 하락이다.
예시) 아래와같이 랜덤숫자가 나왔다고하면
4 8 3 2 6 7 4 5 6 1
1) 처음 4 - 8 은 상승이고 8 다음은 3 이기때문에 최대 상승기간은 1이다.
2) 다시 2 - 6 - 7 은 상승이고 다음 4이기 때문에 최대상승기간은 2이다.
3) 4 - 5 - 6 은 상승이고 6 다음 1이기때문에 최대상승기간은 2이다.
4) 같은 수치가 연속이면 상승으로 치지않는다.
예시 정답) 2일
Random rnd = new Random();
int rNum;
int combo = 0;
int max = 0;
int maxCombo = 0;
for (int i = 0; i < 10; i++ ) {
rNum= rnd.nextInt(8)+1;
System.out.print(rNum + " " );
if( rNum > max) { // rNum이 max값보다 크면
max = rNum; // max는 rNum
combo += 1; // combo값,, 음,,,
if (combo > maxCombo) {
maxCombo = combo-1; // 작은 수 만났을때 리셋됨, 리셋된 수 제외하고 세기
}
} else if (rNum < max) { // max 보다 작을경우 초기화
max = 0;
combo = 0;
}
}
System.out.println();
System.out.println(maxCombo);
정리. 가위바위보 - 반복문 밖 (1~3) / 묵찌빠 - 반복문 안 (4~9)
필요 변수 - 플레이어, cpu, 승부, 승부판단 횟수 모름 - while문
import java.util.Random;
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
int player, cpu;
boolean run = true;
boolean draw = false;
boolean playerAttack = false, cpuAttack = false;
System.out.println("선공후공 정하기 처음에는 가위바위보");
// 플레이어 선택
System.out.println("[1] 가위 [2] 바위 [3]보");
player = sc.nextInt();
cpu = rnd.nextInt(3)+1;
System.out.print("cpu 선택 :" + cpu);
// 처음 선공 정하기 --> 비길 경우 draw 로 선공 정해질때까지 반복 --> 반복문 조건 맞게 수정 -->수정완료
System.out.println();
if (player == 1) {
switch (cpu) {
case 1:
System.out.println("양측 다 가위 다시 내기");
draw = true;
break;
case 2:
System.out.println("플레이어 가위 / 컴퓨터 - 바위");
System.out.println("컴퓨터 선공!");
cpuAttack = true;
break;
case 3:
System.out.println("플레이어 가위 / 컴퓨터 - 보");
System.out.println("플레이어 선공!");
playerAttack = true;
break;
}
} else if (player == 2) {
switch (cpu) {
case 1:
System.out.println("플레이어 바위 / 컴퓨터 - 가위");
System.out.println("플레이어 선공!");
playerAttack = true;
break;
case 2:
System.out.println("플레이어 바위 / 컴퓨터 바위");
System.out.println("양 측다 바위 다시내기");
draw = true;
break;
case 3:
System.out.println("플레이어 바위 / 컴퓨터 - 보");
System.out.println("컴퓨터 선공!");
cpuAttack = true;
break;
}
} else if (player == 3) {
switch (cpu) {
case 1:
System.out.println("플레이어 보 / 컴퓨터 - 가위");
System.out.println("컴퓨터 선공!");
cpuAttack = true;
break;
case 2:
System.out.println("플레이어 보 / 컴퓨터 - 바위");
System.out.println("플레이어 선공!");
playerAttack = true;
break;
case 3:
System.out.println("플레이어 보 / 컴퓨터 - 보");
System.out.println("양 측 다 보 다시내기");
draw = true;
break;
}
} else {
System.err.println("아~~~첫 입력부터 망했어요~~~~ 판 엎습니다~~~~~~~~");
run = false;
}
// 묵찌빠 반복문
while (run) {
// 첫 가위바위보 비길 경우 들어옴
while (draw) {
// 플레이어 재선택
System.out.println("선공후공 쇼부 비김");
System.out.println("[1] 가위 [2] 바위 [3]보");
player = sc.nextInt(); // player 가위바위보 재선택
cpu = rnd.nextInt(3) + 1; // cpu 가위바위보 재선택
System.out.print("cpu 선택 :" + cpu);
System.out.println();
// 선공후공 가리기 --> 1.가위 2.바위. 3보 / 비기면 다시
if (player == 1) {
switch (cpu) {
case 1:
System.out.println("양측 가위 다시내기");
draw = true;
break;
case 2:
System.out.println("플레이어 가위 / 컴퓨터 - 바위 ");
System.out.println("선공 - cpu");
cpuAttack = true;
draw = false;
break;
case 3:
System.out.println("선공 - player");
playerAttack = true;
draw = false;
break;
}
} else if (player == 2) {
switch (cpu) {
case 1:
System.out.println("선공 - player");
playerAttack = true;
draw = false;
break;
case 2:
System.out.println("양측 바위 다시내기");
draw = true;
break;
case 3:
System.out.println("선공 - cpu");
cpuAttack = true;
draw = false;
break;
}
} else if (player == 3) {
switch (cpu) {
case 1:
System.out.println("선공 - cpu");
cpuAttack = true;
draw = false;
break;
case 2:
System.out.println("선공 - player");
playerAttack = true;
draw = false;
break;
case 3:
System.out.println("양측 보 다시내기");
draw = true;
break;
}
}
System.out.println();
}
// 플레이어 공격
/* player와 cpu 재입력 받아 판단
* 비길경우 - 승리조건 만족 - run = false, playerAttack = false
* 이길경우 공격유지 playerAttack = true
* 질 경우 공격 넘기기 cpuAttack = true, playerAttack = false
*/
while (playerAttack) {
System.out.println();
System.out.println("player 공격 ");
System.out.println("묵찌빠 묵찌빠");
System.out.println("[1]찌 [2]묵 [3]빠");
player = sc.nextInt(); // 플레이어 공격 --> 이겼을때 재입력받기
cpu = rnd.nextInt(3)+1; // 컴퓨터 방어
if (player == 1) { // 1.가위, 바위, 보
switch (cpu) {
case 1:
System.out.println("플레이어 찌 / 컴퓨터 - 찌");
System.err.println("이김 - player 승리");
playerAttack = false;
run = false;
break;
case 2:
System.out.println("플레이어 찌 / 컴퓨터 - 묵 ");
System.out.println("공격 컴퓨터로 넘어감 - 플레이어 공격 끝");
playerAttack = false;
cpuAttack = true;
break;
case 3:
System.out.println("플레이어 찌 / 컴퓨터 - 빠");
System.out.println("플레이어 공격 유지"); // 공격 유지의 경우 값을 따로 안줘도 공격 true값 유지
break;
}
} else if (player == 2) {
switch (cpu) {
case 1:
System.out.println("플레이어 묵 / 컴퓨터 - 찌");
System.out.println("플레이어 공격 유지");
break;
case 2:
System.out.println("플레이어 묵 / 컴퓨터 - 묵");
System.err.println("플레이어 승리!");
playerAttack = false;
run = false;
break;
case 3:
System.out.println("플레이어 묵 / 컴퓨터 - 빠");
System.out.println("공격 컴퓨터로 넘어감 - 플레이어 공격 끝");
playerAttack = false;
cpuAttack = true;
break;
}
} else if (player == 3) {
switch (cpu) {
case 1:
System.out.println("플레이어 빠 / 컴퓨터 - 찌");
System.out.println("공격 컴퓨터로 넘어감 - 플레이어 공격 끝");
playerAttack = false;
cpuAttack = true;
break;
case 2:
System.out.println("플레이어 빠 / 컴퓨터 - 묵");
System.out.println("플레이어 공격 유지");
break;
case 3:
System.out.println("플레이어 빠 / 컴퓨터 - 빠");
System.err.println("플레이어 승리!");
playerAttack = false;
run = false;
break;
}
} else {
System.err.println("잘못입력 다시 선택");
continue;
}
}
// 컴퓨터 공격
while (cpuAttack) {
System.out.println();
System.out.println("cpu 공격");
System.out.println("묵찌빠 묵찌빠");
System.out.println("[1]찌 [2]묵 [3]빠");
System.out.print("플레이어 입력 : ");
player = sc.nextInt(); // 플레이어 방어
cpu = rnd.nextInt(3)+1; // 컴퓨터 공격 --> 이겼을때 재입력
System.out.println("컴퓨터 [1]찌 [2]묵 [3]빠 컴퓨터 선택 : " + cpu);
if (cpu == 1) {
switch (player) {
case 1:
System.out.println("컴퓨터 찌 / 플레이어 - 찌");
System.err.println("컴퓨터 승리!");
cpuAttack = false;
run = false;
break;
case 2:
System.out.println("컴퓨터 찌 / 플레이어 - 묵");
System.out.println("공격 플레이어로 넘어감 - 컴퓨터 공격 끝!");
playerAttack = true;
cpuAttack = false;
break;
case 3:
System.out.println("컴퓨터 찌 / 플레이어 - 빠");
System.out.println("컴퓨터 공격 계속");
break;
}
} else if (cpu == 2) {
switch (player) {
case 1:
System.out.println("컴퓨터 묵 / 플레이어 - 찌");
System.out.println("컴퓨터 공격 계속");
break;
case 2:
System.out.println("컴퓨터 묵 / 플레이어 - 묵");
cpuAttack = false;
run = false;
System.err.println("컴퓨터 승리!");
break;
case 3:
System.out.println("컴퓨터 묵 / 플레이어 - 빠");
System.out.println("공격 플레이어로 넘어감 - 컴퓨터 공격 끝!");
playerAttack = true;
cpuAttack = false;
break;
}
} else if (cpu == 3) {
switch (player) {
case 1:
System.out.println("컴퓨터 빠 / 플레이어 - 찌");
System.out.println("공격 플레이어로 넘어감 - 컴퓨터 공격 끝!");
playerAttack = true;
cpuAttack = false;
break;
case 2:
System.out.println("컴퓨터 빠 / 플레이어 - 묵");
System.out.println("컴퓨터 공격 계속");
break;
case 3:
System.out.println("컴퓨터 빠 / 플레이어 - 빠");
cpuAttack = false;
run = false;
System.err.println("컴퓨터 승리!");
break;
}
} else {
System.err.println("뭔가가..뭔가가.....잘못됐다....");
}
}
}
덤. 단일 반복문 과제는 할만한듯? 배열.. 배열이 걱정이다...열심히 하자....