for문, while문을 바로 빠져나간다.
for문의 증감식, while문의 조건식으로 이동한다.


public void rspaper() {
Scanner sc = new Scanner(System.in); //스캐너를 불러와준다.
Random r = new Random(); //랜덤 숫자를 불러와준다
System.out.println("=== 가위 바위 보 게임 ===");
int myhand = 0; // 변수 초기화 해준다.
System.out.println("======result======");
String myStr = ""; //"가위","바위","보"를 쓸 String을 불러온다
while (true) {
System.out.println("숫자를 선택하세요(1.가위 /2.바위 /3.보) ");
myhand = sc.nextInt(); // 숫자를 선택한다
if (myhand == 1) {
myStr = "가위";
} else if (myhand == 2) {
myStr = "바위";
} else if (myhand == 3) {
myStr = "보";
} else {
System.out.println("잘못 입력하셨습니다."); // 1,2,3 외의 숫자를 입력할 수가 있으므로
continue; // 1,2,3 이외의 숫자가 나오면 처음 조건식으로 돌아간다
}
break; // 1,2,3 중에 입력을 하면 이 while문을 벗어난다. 다음으로 넘어감
}
int comhand = r.nextInt(3) + 1;
String comStr = ""; //"가위","바위","보"를 쓸 String을 불러온다
if (comhand == 1) {
comStr = "가위";
} else if (comhand == 2) {
comStr = "바위";
} else if (comhand == 3) {
comStr = "보";
} //컴퓨터는 사람처럼 입력실수를 하지 않으므로 else-continue문이 필요없다
System.out.println("=========결과=========");
System.out.println("당신은" + " "+myStr + "를 냈습니다.");
System.out.println("컴퓨터는" + " "+comStr + "를 냈습니다.");
if (myhand == comhand) {
System.out.println("비겼습니다.");
} else if ((myhand == 2 && comhand == 1) || (myhand == 3 && comhand == 2) || (myhand == 1 && comhand == 3)) {
System.out.println("당신이 이겼습니다 ㅠ.ㅠ");
} else {
System.out.println("컴퓨터가 이겼습니다 >0<");
}
}
public void rspaper() {
Scanner sc = new Scanner(System.in);
Random r = new Random();
boolean bool = true; //게임 전체 진행용 변수
System.out.println("=== 가위 바위 보 게임 ===");
int win = 0;
int lose= 0;
int draw = 0;
while(bool) {
System.out.println("===========가위 바위 보 게임===============");
System.out.println("1.가위바위보 게임 시작");
System.out.println("2.전적확인");
System.out.println("3.게임종료");
System.out.println("선택>");
int sel = sc.nextInt();
switch(sel) {
case 1:
int myhand = 0;
System.out.println("=======result=======");
String myStr = "";
while (true) {
System.out.println("숫자를 선택하세요(1.가위 /2.바위 /3.보) ");
myhand = sc.nextInt();
if (myhand == 1) {
myStr = "가위";
} else if (myhand == 2) {
myStr = "바위";
} else if (myhand == 3) {
myStr = "보";
} else {
System.out.println("잘못 입력하셨습니다.");
continue;
}
break;
}
int comhand = r.nextInt(3) + 1;
String comStr = "";
if (comhand == 1) {
comStr = "가위";
} else if (comhand == 2) {
comStr = "바위";
} else if (comhand == 3) {
comStr = "보";
}
System.out.println("=========결과=========");
System.out.println("당신은" + " "+myStr + "를 냈습니다.");
System.out.println("컴퓨터는" + " "+comStr + "를 냈습니다.");
if (myhand == comhand) {
System.out.println("비겼습니다.");
} else if ((myhand == 2 && comhand == 1) || (myhand == 3 && comhand == 2) || (myhand == 1 && comhand == 3)) {
System.out.println("You Win!!");
win++;
} else {
System.out.println("You Lose!!");
lose++;
}
break;
case 2:
System.out.println("승리 : "+win);
System.out.println("패배 : "+lose);
System.out.println("무승부 : "+draw);
break;
case 3:
System.out.println("잘가요!");
bool = false; // 전체메뉴 부한반복을 종료하기위한 조건
break;
default :
System.out.println("잘못 입력하셨습니다.");
break;
}
}
}
https://velog.velcdn.com/images/bridget0629/post/b44df632-5e65-4130-ae1b-bd8dbcb4a672/image.png)
public class RandomGame {
public void updown() {
Scanner sc = new Scanner(System.in);
Random r = new Random(); // while안에 넣으면 계속 랜덤값이 최소화되서 무한으로 게임을 한다.
boolean bool = true;
int rank = 0;
while(bool) {
System.out.println("============Up & Down Game=========");
System.out.println("1. Game Start");
System.out.println("2. Game Score");
System.out.println("3. Game End");
System.out.println("선택 > ");
int sel = sc.nextInt();
switch(sel) {
case 1:
int count = 1;
System.out.println("<<Game Start>>");
int rdNum = r.nextInt(99)+1; //while문 안에 넣으면 새로 반복될때마다 숫자가 새로 정해져서 밖에 설정
while(true) {
System.out.println(count+"회차 번호입력 :");
int myNum=sc.nextInt();
if(myNum==rdNum) {
System.out.println("정답!");
if(rank == 0 || count<rank) {
rank = count;
}
break;
}else if(myNum> rdNum) {
System.out.println("<<DOWN>>");
}else {
System.out.println("<<UP>>");
}
count++;
}
break;
case 2:
if(rank==0) {
System.out.println("기록이 없습니다");
}else {
System.out.println("최고 기록은"+rank+"회 입니다.");
}
break;
case 3:
System.out.println("게임 종료");
bool = false;
break;
default :
System.out.println("잘못 입력하셨습니다.");
break;
}
}
}