do while循环中的多个输出,具有嵌套的if / else结构

时间:2014-02-26 08:56:58

标签: java do-while

这里有点困惑。如果我取消对System.out.println语句的注释,我会收到错误"否则没有"我需要重新组织我的代码还是在这里有一个简单的解决方案。使用freeTTS API。

     do//begin do loop
    {

    if //if else structure within post test loop
            (guess == computerNumber)
            //System.out.println("Congratulations you guessed correctly!");
            voice.speak("congrats you are correct");
    else if
            (guess < computerNumber)
            //System.out.println("Sorry, your guess was to low. Try Again.");
            voice.speak("sorry, too low");
    else if
            (guess > computerNumber)
            //5System.out.println("Sorry, your guess was to high. Try Again.");
            voice.speak("too high ,bro-ski! go lower!!");
            guess = keyboard.nextInt();
    } 
    while (guess !=computerNumber);//end post test loop
    voice.speak("nice. you finally guessed the correct answer. But, you should always start with 42 since that is the answer to the life the universe and everything");

3 个答案:

答案 0 :(得分:0)

{} bracket

中使用if-else表示多个语句
if(guess == computerNumber){
    System.out.println("Congratulations you guessed correctly!");
    voice.speak("congrats you are correct");
 }else if (guess < computerNumber) {
    System.out.println("Sorry, your guess was to low. Try Again.");
    voice.speak("sorry, too low");
} else if (guess > computerNumber) {
    System.out.println("Sorry, your guess was to high. Try Again.");
    voice.speak("too high ,bro-ski! go lower!!");
    guess = keyboard.nextInt();
}

答案 1 :(得分:0)

您需要在{}

中放置If块和else块

答案 2 :(得分:0)

按照jls,在14.9.7中

如果值为true,则第一个包含的Statement(在else之前的那个) 关键字)被执行;如果和,则if-then-else语句正常完成 只有当该语句的执行正常完成时才会完成。

值表示为if。

块是一系列语句,本地类声明和局部变量 大括号内的声明声明。

Block: { BlockStatementsopt }

所以你要在括号内给出这两行。如果不是只考虑第一行。传递下一行,当编译器看到else部分需要if时,但我们没有与之关联的部分,因此报告错误。