生肖表:生日的标志?

时间:2014-10-15 17:45:11

标签: java

所以,对于我的介绍编程课,我们正在制作一个生肖表。它没有特定年份和闰年,所以这很好。此外,我们正在从.txt文件进行输入,因此我们不必担心用户输入。无论如何,有4列,Zodiac列需要按日期定义。因此,癌症定义为6月22日至7月22日。我有几个月以及它们定义如下:

case 1: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "January";
                    else 
                        validData = false;
                break;
            case 2: if (dayNum >= 1 || dayNum <= 28)
                        dateWord = "February";
                    else 
                        validData = false;
                    break;
            case 3: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "March";
                    else 
                        validData = false;
                    break;
            case 4: if (dayNum >= 1 || dayNum <= 30)
                        dateWord = "April";
                    else 
                        validData = false;
                    break;  
            case 5: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "May";
                    else 
                        validData = false;
                    break;
            case 6: if (dayNum >= 1 || dayNum <= 30)
                        dateWord = "June";
                    else 
                        validData = false;
                    break;
            case 7: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "July";
                    else 
                        validData = false;
                    break;
            case 8: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "August";
                    else 
                        validData = false;
                    break;
            case 9: if (dayNum >= 1 && dayNum <= 30)
                        dateWord = "September";
                    else 
                        validData = false;
                    break;
            case 10: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "October";
                    else 
                        validData = false;
                    break;  
            case 11: if (dayNum >= 1 || dayNum <= 30)
                        dateWord = "November";
                    else 
                        validData = false;
                    break;
            case 12: if (dayNum >= 1 || dayNum <= 31)
                        dateWord = "December";
                    else 
                        validData = false;
                    break;
            default: dateWord = "";

但是,我如何列出某人的星座?如果.txt文件说我的生日是6(月份列)22(日期)列,我如何将他们的星座放在“十二生肖”栏中?此外,如果有一个以上的生日,我如何列出多个标志?

4 个答案:

答案 0 :(得分:1)

从表中获取月份和日期。对所有元素执行此操作并将黄道带标记放入表中。

    int month = 2;
    int day = 23;
    ArrayList<String> zodiacSign = new ArrayList<String>();
    switch (month) {
    case 1:
        if (day < 20) {
            zodiacSign.add("Capricorn");
        } else {
            zodiacSign.add("Aquarius");
        }
        break;
    case 2:
        if (day < 18) {
            zodiacSign.add("Aquarius");
        } else {
            zodiacSign.add("Pisces");
        }
        break;
    case 3:
        if (day < 21) {
            zodiacSign.add("Pisces");
        } else {
            zodiacSign.add("Aries");
        }
        break;
    case 4:
        if (day < 20) {
            zodiacSign.add("Aries");
        } else {
            zodiacSign.add("Taurus");
        }
        break;
    case 5:
        if (day < 21) {
            zodiacSign.add("Taurus");
        } else {
            zodiacSign.add("Gemini");
        }
        break;
    case 6:
        if (day < 21) {
            zodiacSign.add("Gemini");
        } else {
            zodiacSign.add("Cancer");
        }
        break;
    case 7:
        if (day < 23) {
            zodiacSign.add("Cancer");
        } else {
            zodiacSign.add("Leo");
        }
        break;
    case 8:
        if (day < 23) {
            zodiacSign.add("Leo");
        } else {
            zodiacSign.add("Virgo");
        }
        break;
    case 9:
        if (day < 23) {
            zodiacSign.add("Virgo");
        } else {
            zodiacSign.add("Libra");
        }
        break;
    case 10:
        if (day < 23) {
            zodiacSign.add("Libra");
        } else {
            zodiacSign.add("Scorpio");
        }
        break;
    case 11:
        if (day < 22) {
            zodiacSign.add("Scorpio");
        } else {
            zodiacSign.add("Sagittarius");
        }
        break;
    case 12:
        if (day < 22) {
            zodiacSign.add("Sagittarius");
        } else {
            zodiacSign.add("Capricorn");
        }
        break;

答案 1 :(得分:1)

我想出了一个较短的版本,在网络上找不到它,因此决定在此处发布:

// Months are in ints - January is 1, February 2, etc.
public String getZodiac(int day, int month){
        switch(month){
            case 1:
                // if the day int is higher or equal to 20, return Aquarius,
                // otherwise Capricorn
                return day >= 20 ? "Aquarius" : "Capricorn";
            case 2:
                return day >= 19 ? "Pisces" : "Aquarius";
            case 3:
                return day >= 21 ? "Aries" : "Pisces";
            case 4:
                return day >= 20 ? "Taurus" : "Aries";
            case 5:
                return day >= 21 ? "Gemini" : "Taurus";
            case 6:
                return day >= 21 ? "Cancer" : "Gemini";
            case 7:
                return day >= 23 ? "Leo" : "Cancer";
            case 8:
                return day >= 23 ? "Virgo" : "Leo";
            case 9:
                return day >= 23 ? "Libra" : "Virgo";
            case 10:
                return day >= 23 ? "Scorpio" : "Libra";
            case 11:
                return day >= 22 ? "Sagittarius" : "Scorpio";
            case 12:
                return day >= 22 ? "Capricorn" : "Sagittarius";
            // A default option because the function needs a return value
            default:
                return "Error";
        }
    }

答案 2 :(得分:0)

一个更优雅的版本,具有最小的重复。学习基础知识可能不好,但是它应该向您显示,如果您的代码看起来正在重复很多,那么有一种方法可以保留DRY :< / p>

public static final TreeMap<Integer, String> SIGNS;
static {
    // map initialization is only performed once
    SIGNS = new TreeMap<>();
    SIGNS.put( 1_01, "Capricorn");
    SIGNS.put( 1_20, "Aquarius");
    SIGNS.put( 2_18, "Pisces");
    SIGNS.put( 3_21, "Aries");
    SIGNS.put( 4_20, "Taurus");
    SIGNS.put( 5_21, "Gemini");
    SIGNS.put( 6_21, "Cancer");
    SIGNS.put( 7_23, "Leo");
    SIGNS.put( 8_23, "Pisces");
    SIGNS.put( 9_23, "Libra");
    SIGNS.put(10_23, "Scorpio");
    SIGNS.put(11_22, "Sagittarius");
    SIGNS.put(12_22, "Capricorn");
}
public static String zodiacSign(int day, int month) {
    int key = month * 100 + day; // 10 feb = 210 = 2_10
    return SIGNS.floorEntry(key).getValue();
}

TreeMaps是Java的默认排序映射。 TreeMap中的floorKey返回

与小于或等于给定键的最大键相关联的键-值映射

哦,这是Java支持作为数字一部分的任意数量_的一个好用例。

答案 3 :(得分:-1)

Scanner in = new Scanner (System.in);
System.out.print("Enter Month: ");
String m = in.nextLine();
int d;
String I = "Invalid";
String D = "Enter Date: ";
if (m.equals("Jan")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=19){
        System.out.println("Capricorn");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=20){
        System.out.println("Aquarius");
}
}
else if (m.equals("Feb")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=18){
        System.out.println("Aquarius");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=19){
        System.out.println("Pisces");
}
}
else if (m.equals("Mar")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=20){
        System.out.println("Pisces");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=21){
        System.out.println("Aries");
}
}
else if (m.equals("Apr")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=19){
        System.out.println("Aries");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=20){
        System.out.println("Taurus");
}
}
else if (m.equals("May")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=20){
        System.out.println("Taurus");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=21){
        System.out.println("Gemini");
}
}
else if (m.equals("Jun")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=20){
        System.out.println("Gemini");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=21){
        System.out.println("Cancer");
}
}
else if (m.equals("Jun")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=20){
        System.out.println("Gemini");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=21){
        System.out.println("Cancer");
}
}
else if (m.equals("Jul")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=22){
        System.out.println("Cancer");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=23){
        System.out.println("Leo");
}
}
else if (m.equals("Aug")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=22){
        System.out.println("Leo");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=23){
        System.out.println("Virgo");
}
}
else if (m.equals("Sep")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=22){
        System.out.println("Virgo");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=23){
        System.out.println("Libra");
}
}
else if (m.equals("Oct")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=22){
        System.out.println("Libra");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=23){
        System.out.println("Scorpio");
}
}
else if (m.equals("Nov")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=21){
        System.out.println("Scorpio");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=22){
        System.out.println("Sagittarius");
}
}
else if (m.equals("Dec")){
    System.out.print(D);
    d = in.nextInt();
        if (d<=0){
        System.out.println(I);
}
        else if (d<=21){
        System.out.println("Sagittarius");
}
        else if (d>=32){
        System.out.println(I);
}
        else if (d>=22){
        System.out.println("Capricorn");
}
}
else{
    System.out.println(I);
}