使用多个if else语句以及它们循环的原因?

时间:2015-02-18 19:12:11

标签: java eclipse

当我输入这段代码时,它会给我一个回报,一旦我第一次使用yes,就名称而言,它会为所有这些使用该回报,例如:如果我说我的名字是Justin,我说那是"是"是的,然后说我输错了我的年龄,那么它不会给我一个改变它的机会,它会说"太棒了!"。我该如何解决?我在9年级,所以我不太了解编程,对于noob问题感到抱歉。提前谢谢!

import java.util.Scanner;
public class helloworld_main {
private static Scanner scan;

public static void main(String args[])
{

    scan = new Scanner(System.in);
    String name, age, year, yes, no, no1, no2;

    System.out.print("Please enter your name --> "); // user prompt
    name = scan.nextLine();

    System.out.print("Please enter your age --> "); // user prompt
    age = scan.nextLine();

    System.out.print("Please enter the year you were born --> "); // user prompt
    year = scan.nextLine();


    // Their Name
    System.out.println("So your name is... " + name + ". Right?"); // correction if name is not correct
    yes = scan.nextLine();

        if ("Yes".equals(yes)) 
        {
                System.out.println("Great!");
         } else {
                    System.out.println("Oh. Please retype it.");
                    no = scan.nextLine();
    System.out.println("Hello, " + no);
         }

        // The Age
    System.out.println("The age you entered is..." + age + ". Right?");

        if ("Yes".equals(yes))
        {
            System.out.println("Great!");
        } else {
                System.out.println("OK. Please reenter your age.");
                no1 = scan.nextLine();
        System.out.println("OK, I love " + no1 + " year olds!");
        }

        // Year Born
    System.out.println("The year you were born is... " + year + ". Right?");

        if ("Yes".equals(yes))
        {
            System.out.println("Fantastic!");
        } else {
                System.out.println("Ok then, please tell me what year you were born again.");
                no2 = scan.nextLine();
        System.out.println("Cool! I know someone else born in " + no2);
        }
    scan.close();

}

}

1 个答案:

答案 0 :(得分:1)

再次提示后,您需要再次阅读用户输入。添加:

yes = scan.nextLine();

后:

System.out.println("The age you entered is..." + age + ". Right?");

System.out.println("The year you were born is... " + year + ". Right?");
相关问题