如何在if语句中修复if语句?

时间:2014-11-28 02:19:30

标签: 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”? -Solved -

新问题。以下是我尝试修复脚本后脚本的最后一部分。 我相信问题是在终止之前的代码块中发生的,但是我不确定究竟是什么,因为我尝试应用上述修复并且它不起作用。 (在“感谢您参与”代码上方的其他声明)已解决

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

-Note - 这是在应用第一个“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)

1 个答案:

答案 0 :(得分: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.");
    }
}