有人可以指导我做我的hw。它关于枚举。

时间:2015-08-09 11:27:23

标签: java if-statement random enums

使用枚举为以下值创建程序。 3个对象 名称: 年龄: 状态: *如果单身,你的程序应该打印出单一的随机原因: 等待是徒劳的 2.理所当然 3.选择

1 个答案:

答案 0 :(得分:-1)

as far as I understand, the only candidate for enum is the status. 
public enum Status{

SINGLE,MARRIED,WIDOW ...
}


public class Person{
    String[] reasons = {"Waiting in vain","Taken for granted","Choosy"};
    Status status;
    int age;
    String name;
    Random random = new Random();

    public Person(Status status, int age, String name) {
        this.status = status;
        this.age = age;
        this.name = name;
    }

    public printRandomValue() {
        if (status == Status.SINGLE) {
            int index = random.nextInt(reasons.length);
            System.out.println(reasons[index]);
        }
    }
}