mercredi 24 juillet 2019

A bit confused on my if loop not running?

I'm pretty new to programming and new to this site, so bear with me if this explanation is clunky.

I'm teaching myself java programming, and currently I'm just writing up a general little code to practice the use of classes and methods. It's a simple code just asking for name and age and if one has a pet to just practice.

I have an if loop for when asked if the person has a pet, if they say yes, it should do one thing, no another, and for anything else it should say more or less 'hey, i didn't understand that', and move on.

However when I ask 'do you have a pet', it doesn't give me a chance to even input information before going right to the 'else' section of my loop, and I'm not sure why.

I'm very new to programming so I'm not sure what to do or try. I know I'm missing something silly, but i don't know what.

package practice;

import java.util.Scanner;

class Person {

    String name;
    int age;

    void sayInfo() {

        System.out.println("Hey, my name is " + name + " and I'm " + age + " years old!");

    }

}

class Pet {
    String name;
    String type;
    int age;
}

public class Methods {
    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        Person person1 = new Person();
        String answer2;

        do {

            System.out.println("Please state your name:");
            person1.name = input.nextLine();
            System.out.println("Please state your age:");
            person1.age = input.nextInt();

            System.out.println("Do you have a pet?");
            String answer = input.nextLine();

            if (answer.equals("yes") || answer.equals("Yes")) {

                Pet pet1 = new Pet();

                System.out.println("What kind of pet?");
                pet1.type = input.nextLine();
                System.out.println("What is their name?");
                pet1.name = input.nextLine();
                System.out.println("How old are they?");
                pet1.age = input.nextInt();

                System.out.println(
                        "okay, so your name is " + person1.name + ", you're " + person1.age + "years old, and");
                System.out.println(
                        "You have a " + pet1.type + " named " + pet1.name + " who is " + pet1.age + "years old.");

            }

            else if (answer.equals("no") || answer.equals("No")) {

                System.out.println("okay, so your name is " + person1.name + ", you're " + person1.age
                        + "years old, and you have no pet.");
            }

            else {

                System.out.println("Sorry, I didn't understand your input there.");
            }

            System.out.println("Is your information correct?");
            answer2 = input.nextLine();

        } while (answer2.equals("no") || answer2.equals("No"));

        System.out.println("Thank you for your information and time.");
    }

}

When I run the program, it'll ask for name and age just fine, but when it gets to 'do you have a pet', it just doesn't let me input and goes right to the else part of the loop, "Sorry, I didn't understand your input there", but the rest of the program, asking if the information is correct and rerunning if not seems to work fine, it's just that one part that has me stumped.

Aucun commentaire:

Enregistrer un commentaire