如何在不绕过另一个代码的情况下执行代码块? Java的

时间:2014-02-26 22:14:23

标签: java

标题可能不是很有用。但这是交易。

我希望用户在第二次通过代码时执行代码。我做了什么,正在制作一个if语句,但现在我注意到了,那个,剩下的就没有被执行了。

Scanner input = new Scanner(System.in);
Random dice = new Random();
int counter = 1;
boolean playing = true;
boolean firstTimmer = true;
boolean got = true;

System.out.println("Welcome to numberMind! From 0 to x, you'll try to guess the random number!");
System.out.println("To quit, guess \"-1\".");
System.out.print("Insert x: ");
int x = 1+input.nextInt();
int objective = dice.nextInt(x);
System.out.print("Ok, I'm ready! What's your first guess? ");
int guess = input.nextInt();


while (playing){
    if (guess == -1){
        playing = false;
        break;
    }else if (!firstTimmer){
        got = true;
        System.out.print("Do you want to change the number range? Yes(1) No(2)");
        guess = input.nextInt();
        if (guess == 1){
            System.out.print("Insert the new x: ");
            x = 1+input.nextInt();
        }else if(guess == 2){
            System.out.println("Let's go then!");
        }else{
            System.out.print("I didn't ask for that number did I? x won't change.");
        }
    }else{
        while(got){
            if (objective == guess){
                firstTimmer = false;
                System.out.println("You guessed it in "+counter+" times!");
                counter = 1;
                System.out.print("Do you want to paly again? Yes(1) No(2) ");
                guess = input.nextInt();
                if (guess == 1){
                    System.out.println("Great! Here we go...");
                    got = false;
                    break;
                }else if (guess == 2){
                    System.out.print("Thanks for playing!");
                    got = false;
                    playing = false;
                }else{
                    System.out.println("We didn't ask for that. NOW YOU PLAY SOME MORE!");
                    got = false;
                    break;
                }
                break;
            }else if (guess == -1){
                System.out.println("You quited :(");
                break;
            }else if (guess == -2){
                System.out.println("The correct answer is "+ objective);
            }else if (counter >= 5 && (counter -5) % 3 == 0 ){
                if (objective % 2 == 0){
                    System.out.println("The number is pair.");
                }else{
            System.out.println("The number is odd.");
                }
            }
            System.out.println("You have tryed " + counter++ + " times.");
            System.out.print("What's your guess? ");
            guess = input.nextInt();
        }
    }
}

我想要运行的代码是在最后一个之后。我没有看到任何解决方法。谢谢

4 个答案:

答案 0 :(得分:2)

首先问自己什么时候会else完成:guess != -1firstTimmer == true。您没有设置firstTimmer,需要将其设置为true才能通过else块。您还需要移除break中的else if,否则在下一次迭代中它永远不会到达其他地方。

此外,在playing = false语句中同时包含breakif也是多余的。两者都会单独做同样的事情。

while (playing){
    if (guess == -1){
        playing = false;
        break;
    }else if (!firstTimmer){
        got = true;
        System.out.print("Do you want to change the number range? Yes(1) No(2)");
        guess = input.nextInt();
        if (guess == 1){
            System.out.print("Insert the new x: ");
            x = 1+input.nextInt();
        }else if(guess == 2){
            System.out.println("Let's go then!");
        }else{
            System.out.print("I didn't ask for that number, x won't change.");
        }
        firstTimmer = true;
        //break;
    }else{

答案 1 :(得分:1)

int counter = 0;
while(playing)  {
   counter++;      //First iteration: was zero, now 1
   if(counter == 2)  {
      //Do some special thing
   }
}

答案 2 :(得分:1)

(一)此类事情的标准模式是:

boolean firstTime = true;
boolean playing = true;
int guess = 0;
while (playing) {
    if (guess == -1) {
        playing = false;
        // NOTE: break is redundant with playing flag, here
    } else if (!firstTime) {
        got = true;
        System.out.print("Do you want to change the number range? Yes(1) No(2)");
        guess = input.nextInt();
        if (guess == 1) {
            System.out.print("Insert the new x: ");
            x = 1+input.nextInt();
        } else if(guess == 2) {
            System.out.println("Let's go then!");
        } else {
            System.out.print("I didn't ask for that number, x won't change.");
        }
        firstTime = false;
    } else {
        //... do other work here.
    }
}

此外,aliteralmind提到的模式是上述更灵活的版本,允许您通过循环为每次“访问”提供不同的选项。如有必要,我可以用一个完整的例子进一步描述。

答案 3 :(得分:1)

您应该尝试在代码中每次解决一个问题。

现在您有很多难以阅读和使用的复杂代码。

关于第一次访问的问题的答案。只需在循环之前移动它,然后在必要时启动循环。

尝试在子程序(方法)中划分问题并一次解决它们。

displayWelcomeScreen();
preapreGameContext();

do {
 if(doUserWantToChange()) {
   changeTheGameContext();
 }
 int gues = askForGues();

 hasUserGuesCorrectly();

} while(isTheGameOn());

最终代码应该与上面的例子看起来更相似。