-1
컴퓨터 나 플레이어가 5 게임을 승리 한 후 루프를 종료하려고합니다. for 루프를 사용해 보았지만 전혀 멈추지 않았습니다. 내가 디버그하고 그것을 알아 내려고했지만 나는 길을 잃었다. 내가 잘못 된 곳을 찾아 내도록 도와 주시겠습니까? 코드를 더욱 효율적으로 만들 수있는 제안은 매우 감사 할 것입니다.for 루프가 원하는대로 작동하지 않는 이유는 무엇입니까?
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissor {
public static void main (String [] args){
System.out.println("Lets play Rock, Paper, Scissors!");
Scanner scanner = new Scanner(System.in);
int computerwins=0;
int playerwins=0;
Random random = new Random();
for (int totalGame = 0; playerwins<=5 || computerwins<=5; ++totalGame){
int gamenumber = totalGame + 1;
System.out.print("Game " + gamenumber + ". " + "Pick one! 1 = Rock, 2 = Paper, 3 = Scissor : ");
int input = scanner.nextInt();
Random rand = new Random();
int computerResponse = rand.nextInt(3) + 1;
String compResponse = "something";
switch (computerResponse) {
case 1: compResponse = "Rock";
break;
case 2: compResponse = "Paper";
break;
case 3: compResponse = "Scissor";
break; }
String you = "something";
switch (input) {
case 1: you = "Rock";
break;
case 2: you = "Paper";
break;
case 3: you = "Scissor";
break;
}
System.out.println("You: " + you + ", Computer: " + compResponse);
if(input == computerResponse) {
System.out.println("It's a tie!"); }
else if (input == 1) {
if (computerResponse == 2) {
System.out.println("Paper eats rock. You lose!");
++computerwins; }
else if (computerResponse == 3) {
System.out.println("Rock crushes scissors. You win!");
++playerwins; } }
else if (input == 2) {
if (computerResponse == 1) {
System.out.println("Paper eats rock. You win!");
++playerwins; }
else if (computerResponse == 3) {
System.out.println("Scissor cuts paper. You lose!");
++computerwins; } }
else if (input ==3) {
if (computerResponse == 1) {
System.out.println("Rock crushes scissors. You lose!");
++computerwins; }
else if (computerResponse == 2) {
System.out.println("Scissor cuts paper. You win!");
++playerwins; } }
System.out.println("Score Now: You = " + playerwins + ", Computer = " + computerwins);
System.out.println(" "); }
if (playerwins == computerwins) {
System.out.println("Aww! It's a tie."); }
if (playerwins < computerwins) {
System.out.println("Aww! You lost!"); }
if (playerwins > computerwins) {
System.out.println("Yay! You won!");}
} }
감사합니다. 에서
루프 조건에서'||'대신'&&'를 사용하고 싶습니다. 그렇지 않으면 두 플레이어 모두 6 승이 올 때까지 계속됩니다. 또한, 당신이'<='대신에'<'를 원한다고 생각합니다. 그래서'플레이어는 <5 && 컴퓨터 <5' –
조건'playerwins <= 5 || computerwins <= 5'는 한 번만 거짓이 될 것입니다. * 양쪽 * 쪽은 5 번 이상 승리합니다. 만약 당신이 그것을 원한다면, 또는 그것을'playerwins <= 5 && computerwins <= 5'로 변경하십시오. – shmosel
@DavidWallace 그 문제를 다루는 중복 질문을 알고 있습니까? 이런 종류의 질문이 꽤 자주 나타나기 때문에, 반복해서 대답하는 대신 속임수로 그들을 닫는 것이 더 좋을 것입니다. – Tom