【问题标题】:How can i fix an if statement within an if statement?如何在 if 语句中修复 if 语句?
【发布时间】:2015-01-26 15:55:02
【问题描述】:

我正在自学 Java,我已经学习了几个星期,决定制作一个包含多个选项的程序。 第一部分是在两种动物之间进行选择,在本例中是海豹和河马。 在那之后,我在选择印章后遇到的问题是我想要选择印章后的选项,例如重复动作、终止动作或在用户随机输入内容时要求正确输入。

这是我的代码

import java.util.Scanner;

class animal {

    //Animal variables
    String name;
    String type;

    //Method or subroutine, something an object belonging to the class can do
    void sound() {
    System.out.println(name + " was petted! " + name + " went 'Gauuu!'");
    }

}



public class Objectmethod {

    public static void main(String[] args) {

        Scanner scanner1 = new Scanner(System.in);
        Scanner scanner2 = new Scanner(System.in);

        //Creating an object under the class of Animal
        animal animal1 = new animal();
        animal1.name = "Baby Elephant Seal";
        animal1.type = "Seal";


        //Creating second animal
        animal animal2 = new animal();
        animal2.name = "Hippopotawhateveritis";
        animal2.type = "Hippopotamus or however you spell it";


        //Beginning prompt for user input
        System.out.println("Would you like to pet the seal, or the hippo?");

        //The code to recieve input of the user
        String select = scanner1.nextLine();

        //check user input to select correct object
        while(true)
            if(select.equals("seal")){

                //Command that the animal sounds has.
                animal1.sound();

                //Prompt if the user would like to repeat
                System.out.println("Would you like to pet the seal again?");

                //second input recieving from user
                String input = scanner2.nextLine();

                    //Checks user input for yes no or random crap
                    if(input.equals("yes")){
                        continue;
                    }
                    else if(input.equals("no")){
                        break;
                    }
                    else{
                        System.out.println("Answer yes or no you derpface.");
                        input = scanner2.nextLine();

                    }
            }
            else if(select.equals("hippo")){
                animal2.sound();
                break;
            }
            else{
                System.out.println("You cray cray. Just pick one.");
                select = scanner1.nextLine();
            }
        System.out.println("Thank you for participating.");
    }

}

打字印章工作正常 然后当输入胡言乱语以获得“回答是或否你的derpface”的else响应时 它第一次工作,然后第二次返回响应。 这就是发生的事情

Would you like to pet the seal, or the hippo?
seal
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
"randominput"
Answer yes or no you derpface.
"secondrandominput"
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
no
Thank you for participating.

我有什么错误导致它转到“if”而不是“else”? -已解决-

新问题。这是我尝试修复脚本后的最后一部分。 我相信这个问题发生在终止之前的代码块中,但是我不确定它到底是什么,因为我试图应用上述修复并且它没有工作。 (“感谢参与”代码上方的 else 语句)已解决

(问题是将第一个“while(true)”移到其他所有内容之上。我将它移回原来在“scanner1”下方的位置,现在它可以正常运行

-注意-这是在应用第一个“while(true)”来稳健地重复抚摸动物之后。

else{
    while(!select.equals("seal") && !select.equals("hippo")){
    System.out.println("You cray cray. Just pick one.");
    select = scanner1.nextLine();
    }

问题示例:(引号代表用户输入。)

Would you like to pet the seal, or the hippo?
"neither"
You cray cray. Just pick one.
"neither"
You cray cray. Just pick one.
"seal"
Thank you for participating.
Would you like to pet the seal, or the hippo?

我希望它发生的方式:(引号代表用户输入。)

Would you like to pet the seal, or the hippo?
"neither"
You cray cray. Just pick one.
"neither"
You cray cray. Just pick one.
"seal"
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
...
...
//Rest of the program (Confirming repeated petting, or termination of program)

【问题讨论】:

  • 如果我继续输入乱码或无法识别的输入,它会在正确响应和“是”响应之间交替。
  • 我会立即提出的一个建议是摆脱其中一台扫描仪。这里有两个扫描仪没有任何意义。此外,您使用 select 获取初始字符串,但使用不同的字符串在 while 循环中获取新输入,但仍基于原始字符串进行比较。
  • 我不确定是否需要两个。什么时候适合使用多台扫描仪? (包、类、对象..?)
  • 您可能需要多台扫描仪的一个原因是,如果您从多个来源进行扫描(即,使用一台扫描仪从 system.in 扫描,而使用另一台扫描仪从文件扫描)。

标签: java if-statement while-loop


【解决方案1】:

这应该可以解决您的一个问题。如果这不是您想要的,请告诉我。

else{
    while (!input.equals("yes") && !input.equals("no")){
        System.out.println("Answer yes or no you derpface.");
        input = scanner2.nextLine();
    }
}

不过,您还有另一个问题。在while(true) 循环之前轮询用户响应。

注意:在进一步检查您的代码后,似乎该功能是有意的。如果您想让您的代码更加健壮以不断地抚摸所有动物,请参阅下面的代码。

你应该把你的代码编辑成这样:

while(true) {
    System.out.println("Would you like to pet the seal, or the hippo?");

    //The code to recieve input of the user
    String select = scanner1.nextLine();
    .
    .
    .
    // the rest of your code

根据 OP 的要求,这里应该是完整编辑的代码:

import java.util.Scanner;

    class animal {

    // Animal variables
    String name;
    String type;

    // Method or subroutine, something an object belonging to the class can do
    void sound() {
        System.out.println(name + " was petted! " + name + " went 'Gauuu!'");
    }

}

public class Objectmethod {

    public static void main(String[] args) {

        Scanner scanner1 = new Scanner(System.in);
        Scanner scanner2 = new Scanner(System.in);

        // Creating an object under the class of Animal
        animal animal1 = new animal();
        animal1.name = "Baby Elephant Seal";
        animal1.type = "Seal";

        // Creating second animal
        animal animal2 = new animal();
        animal2.name = "Hippopotawhateveritis";
        animal2.type = "Hippopotamus or however you spell it";

        // check user input to select correct object
        while (true) {

            // Beginning prompt for user input
            System.out.println("Would you like to pet the seal, or the hippo?");

            // The code to recieve input of the user
            String select = scanner1.nextLine();

            if (select.equals("seal")) {

                // Command that the animal sounds has.
                animal1.sound();

                // Prompt if the user would like to repeat
                System.out.println("Would you like another animal?");

                // second input recieving from user
                String input = scanner2.nextLine();

                // Checks user input for yes no or random crap
                if (input.equals("yes")) {
                    continue;
                } else if (input.equals("no")) {
                    break;
                } else {
                    while (!input.equals("yes") && !input.equals("no")) {
                        System.out.println("Answer yes or no you derpface.");
                        input = scanner2.nextLine();
                    }
                }
            } else if (select.equals("hippo")) {
                animal2.sound();
                break;
            } else {
                System.out.println("You cray cray. Just pick one.");
                select = scanner1.nextLine();
            }  
        }

        System.out.println("Thank you for participating.");
    }
}

【讨论】:

  • 他的主要问题之一是使用两个单独的字符串但混合比较。当他抚摸着印章,然后说是时,这会扰乱未来的运行,因为他仍在根据仅在循环之前获得的字符串进行比较。
  • 是的,这也是真的。我也会编辑我的答案来解决这个问题。
  • 应用你放在那里的东西,它就像一个魅力。谢谢你。但是我遇到了另一个问题。现在 end 语句无法运行。胡言乱语的反应很好,但是在选择了 seal 之后,它只是跳过了内部的 If 语句并直接走到了最后。我已经尝试像您显示的那样应用“while(...)”,但是它仍然终止到最后一部分,然后继续重新启动整个程序。你想抚摸海豹还是河马?- asdf- 你克雷克雷。随便挑一个。-海豹-感谢您的参与。-您想抚摸海豹还是河马?
  • 我已经编辑了我的答案。这应该可以解决您的问题。如果没有,请告诉我。
  • @DavidJones 您所说的代码正是我设置代码的方式,并且内部 IF 语句的 -else- 回复工作正常。但是 Seal 或 Hippo select 的 Else 回复无法正常运行,并且与之前的 IF 语句存在相同的问题。所以它说“你克雷克雷。随便挑一个”。然后,如果给出“密封”答案,它会跳过“密封”对象中较小的 IF 语句。我试过用“seal”和“hippo”来应用你的修复,但它似乎不是同一个问题,也没有修复。我将编辑我的主要帖子以说明我的意思。
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多