java - 如何修复 if 语句中的 if 语句?

标签 java if-statement while-loop

我正在自学 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”的其他响应 它第一次起作用,然后第二次返回到响应。 这是发生了什么

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)

最佳答案

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

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.");
    }
}

关于java - 如何修复 if 语句中的 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27181038/

相关文章:

javascript - jQuery/Javascript if 语句速度

apache - .htaccess - 如何根据环境变量设置自定义 header ?

C++ 函数在 while(GetMessage(...)){...} 中不起作用

mysql - 如何添加月份列表作为 SQL 条件?

shell - while 循环的输入来自 `command` 的输出

java - Jackcess 等效于具有 > ("strictly greater than") 条件的 SQL WHERE 子句

java - 如何在 Java GUI 程序中获得增量显示

java - java.nio.file.Files类中exists()方法的行为

java - 在 Spark java 中将 JavaRDD 转换为 DataFrame

javascript - 测试值是否在某个范围内时,If 语句始终返回 true