如果用户没有正确地将他们的年龄放入控制台,我该如何再次询问用户的年龄?

时间:2018-05-30 12:21:21

标签: java

我目前正在从一开始就学习Java。

当我做一些练习时,我遇到了这个程序挑战,我无法弄清楚该怎么做。

该程序只询问用户的年龄和姓名。

它检查年龄是否小于0,如果是,则要求用户输入正确的年龄,一旦用户输入了正确的年龄,它就会继续询问用户的姓名

但我无法弄清楚如何实现这一点。

下面是我编写的代码,如果我运行它,它会显示消息"错误的年龄。请输入正确的年龄。"并立即询问用户的姓名。

有人可以指导我吗?

非常感谢。

public class Person {
    private int age;
    private String name;

    public void setAge(int _age) {

        if (_age < 0) {
            System.out.println("Wrong age. Please input correct age.");
            return;
        }

        age = _age;
    }

    public void setName(String _name) {
        name = _name;
    }


    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

}
import java.util.Scanner;

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

        Scanner scan = new Scanner(System.in);

        Person p1 = new Person();
        System.out.println("Please enter the age: ");
        p1.setAge(scan.nextInt());
        System.out.println("Please enter the name: ");
        p1.setName(scan.next());

        Person p2 = new Person();
        System.out.println("Please enter the age: ");
        p2.setAge(scan.nextInt());
        System.out.println("Please enter the name: ");
        p2.setName(scan.next());

        System.out.println(p1.getName() + " / " + p1.getAge());
        System.out.println(p2.getName() + " / " + p2.getAge());
    }
}

2 个答案:

答案 0 :(得分:4)

您可以使用以下代码片段

do {
    System.out.println("Please enter the age: ");
    p1.setAge(scan.nextInt());
} while(p1.getAge() < 0);

答案 1 :(得分:0)

你应该使用循环。

您可以更改setAge()的定义,如下所示:

public boolean setAge(int _age) {

    if (_age < 0) {
        System.out.println("Wrong age. Please input correct age.");
        return false;
    }

    age = _age;
    return true;
}

并呼叫如下:

while(!p1.setAge(scan.nextInt()));
...
while(!p2.setAge(scan.nextInt()));

以下是完整的代码。见working here

class Person {
    private int age;
    private String name;

    public boolean setAge(int _age) {

        if (_age < 0) {
            System.out.println("Wrong age. Please input correct age.");
            return false;
        }

        age = _age;
        return true;
    }

    public void setName(String _name) {
        name = _name;
    }


    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

}

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

        Scanner scan = new Scanner(System.in);

        Person p1 = new Person();
        System.out.println("Please enter the age: ");
        while(!p1.setAge(scan.nextInt()));
        System.out.println("Please enter the name: ");
        p1.setName(scan.next());

        Person p2 = new Person();
        System.out.println("Please enter the age: ");
         while(!p2.setAge(scan.nextInt()));
        System.out.println("Please enter the name: ");
        p2.setName(scan.next());

        System.out.println(p1.getName() + " / " + p1.getAge());
        System.out.println(p2.getName() + " / " + p2.getAge());
    }
}