Java if,else语句与数据

时间:2017-10-15 21:29:33

标签: java if-statement

我是编程的初学者,我遇到了编写黄道带标志程序的麻烦。由于十二生肖与特定数据而非一个月相关联,因此创建代码更加困难。我的问题是:当我输入我的生日和出生月份时,它会向我显示错误的星座。我究竟做错了什么? 这是我的代码:

String day = DD.getText();
int dayinput;
dayinput = Integer.parseInt(day);


String month = MM.getText();
int monthinput;
monthinput = Integer.parseInt(month);

if (monthinput == 12 && dayinput <= 23 || monthinput == 1 && dayinput <= 20) {
    jLabel1.setText("Capricorn");
} else {
    jLabel1.setText("Aquarius");
}

if (monthinput == 1 && dayinput >= 21 || monthinput == 2 && dayinput <= 18) {
    jLabel1.setText("Aquarius");
} else {
    jLabel1.setText("Pisces");
}

if (monthinput == 2 && dayinput >= 19 || monthinput == 3 && dayinput <= 20) {
    jLabel1.setText("Pisces");
} else {
    jLabel1.setText("Aries");
}

if (monthinput == 3 && dayinput >= 21 || monthinput == 4 && dayinput <= 20) {
    jLabel1.setText("Aries");
} else {
    jLabel1.setText("Taurus");
}

if (monthinput == 4 && dayinput >= 21 || monthinput == 5 && dayinput <= 21) {
    jLabel1.setText("Taurus");
} else {
    jLabel1.setText("Gemini");
}

if (monthinput == 5 && dayinput >= 22 || monthinput == 6 && dayinput <= 21) {
    jLabel1.setText("Gemini");
} else {
    jLabel1.setText("Cancer");
}

if (monthinput == 6 && dayinput >= 22 || monthinput == 7 && dayinput <= 23) {
    jLabel1.setText("Cancer");
} else {
    jLabel1.setText("Leo");
}

if (monthinput == 7 && dayinput >= 24 || monthinput == 8 && dayinput <= 23) {
    jLabel1.setText("Leo");
} else {
    jLabel1.setText("Virgo");
}

if (monthinput == 8 && dayinput >= 24 || monthinput == 9 && dayinput <= 23) {
    jLabel1.setText("Virgo");
} else {
    jLabel1.setText("Libra");
}

if (monthinput == 9 && dayinput >= 24 || monthinput == 10 && dayinput <= 23) {
    jLabel1.setText("Libra");
} else {
    jLabel1.setText("Scorpio");
}

if (monthinput == 10 && dayinput >= 24 || monthinput == 11 && dayinput <= 22) {
    jLabel1.setText("Scorpio");
} else {
    jLabel1.setText("Sagittarius");
}

if (monthinput == 10 && dayinput >= 23 || monthinput == 11 && dayinput <= 22) {
    jLabel1.setText("Sagittarius");
} else {
    jLabel1.setText("Capricorn");
}

4 个答案:

答案 0 :(得分:1)

好的,我看看你的问题,并且reviewer我编写更多可读代码比在多个if...else中找到错误更容易。

public static void main(String... args) {
    int day = Integer.parseInt(DD.getText());
    int month = Integer.parseInt(MM.getText());
    ZodiacSign zodiacSign = ZodiacSign.get(day, month);
    JLabel jLabel1 = new JLabel(zodiacSign.name());
    System.out.println(jLabel1.getText());
}

public enum ZodiacSign {
    Aries(21, Calendar.MARCH, 20, Calendar.APRIL),
    Taurus(21, Calendar.APRIL, 21, Calendar.MAY),
    Gemini(22, Calendar.MAY, 21, Calendar.JUNE),
    Cancer(22, Calendar.JUNE, 22, Calendar.JULY),
    Leo(21, Calendar.JULY, 21, Calendar.AUGUST),
    Virgo(22, Calendar.AUGUST, 23, Calendar.SEPTEMBER),
    Libra(24, Calendar.SEPTEMBER, 23, Calendar.OCTOBER),
    Scorpio(24, Calendar.OCTOBER, 22, Calendar.NOVEMBER),
    Sagittarius(23, Calendar.NOVEMBER, 22, Calendar.DECEMBER),
    Capricorn(23, Calendar.DECEMBER, 20, Calendar.JANUARY),
    Aquarius(21, Calendar.JANUARY, 19, Calendar.FEBRUARY),
    Pisces(20, Calendar.FEBRUARY, 20, Calendar.MARCH);

    private final int dayFrom;
    private final int dayTo;
    private final int monthFrom;
    private final int monthTo;

    ZodiacSign(int dayFrom, int monthFrom, int dayTo, int monthTo) {
        this.dayFrom = dayFrom;
        this.monthFrom = monthFrom + 1;
        this.dayTo = dayTo;
        this.monthTo = monthTo + 1;
    }

    public static ZodiacSign get(int day, int month) {
        for (ZodiacSign sign : values())
            if (month == sign.monthFrom && day >= sign.dayFrom
                    || month == sign.monthTo && day <= sign.dayTo)
                return sign;

        throw new RuntimeException("Cannot select ZodiacSign");
    }
}

答案 1 :(得分:0)

dayinput&lt; = 23是错误的,摩羯座是&gt; = 22

if  (monthinput == 12 && dayinput <= 23 || monthinput == 1 && dayinput <= 20 ) 
{
        jLabel1.setText("Capricorn");

检查剩下的符号,else语句在每种情况下都是错误的,用另一个if语句替换。

答案 2 :(得分:0)

错误是几乎总是执行最后的其他操作。 使用if-then-else-if链来拥有独占案例。

    String sign;
    if (month == 12 && day <= 23 || month == 1 && day <= 20) {
        sign = "Capricorn";
    } else if (month == 1 && day >= 21 || month == 2 && day <= 18) {
         sign = "Aquarius";
    } else if (month == 2 && day >= 19 || month == 3 && day <=20) {
         sign = "Pisces";
    } else if (month == 3 && day >= 21 || month == 4 && day <=20) {
         sign = "Aries";
    } else if (month == 4 && day >= 21 || month == 5 && day <= 21) {
         sign = "Taurus";
    } else if (month == 5 && day >= 22 || month == 6 && day <= 21) {
         sign = "Gemini";
    } else if (month == 6 && day >= 22 || month == 7 &&  day <= 23) {
        sign = "Cancer";
    } else if (month == 7 && day >= 24 || month == 8 && day <= 23) {
         sign = "Leo";
    } else if (month == 8 && day >= 24 || month == 9 && day <=23) {
         sign = "Virgo";
    } else if (month == 9 && day >= 24 || month == 10 && day <=23) {
         sign = "Libra";
    } else if (month == 10 && day >= 24 || month == 11 && day <=22) {
         sign = "Scorpio";
    } else if (month == 10 && day >= 23 || month == 11 && day <=22) {
         sign = "Sagittarius";
    } else {
         sign = "Capricorn";
    }
    jLabel1.setText(sign);

答案 3 :(得分:0)

考虑一下:

  • 黄道带标志的范围-使每个范围都像一个带有开始“日期”的步骤。
  • 按照步骤进行-我发现从上边界到下边界更容易。如果您尚未通过一步的“日期”,那么您就在这一步范围之内。

希望这很清楚。

enum ZodiacSign { Aquarius, Pisces, Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn }

public String GetZodiacSign(int month, int day)
{
    Map<Integer,ZodiacSign>m = new LinkedHashMap<Integer,ZodiacSign>();

    m.put(1222,ZodiacSign.Capricorn);
    m.put(1122,ZodiacSign.Sagittarius);
    m.put(1023,ZodiacSign.Scorpio);
    m.put(923 ,ZodiacSign.Libra);
    m.put(823 ,ZodiacSign.Virgo);
    m.put(723 ,ZodiacSign.Leo); 
    m.put(621 ,ZodiacSign.Cancer);
    m.put(520 ,ZodiacSign.Gemini);
    m.put(420 ,ZodiacSign.Taurus);
    m.put(321 ,ZodiacSign.Aries);
    m.put(219 ,ZodiacSign.Pisces);
    m.put(120 ,ZodiacSign.Aquarius);
    m.put(101 ,ZodiacSign.Capricorn); // loop back

    int position = month * 100 + day;

    for (Map.Entry<Integer, ZodiacSign> entry : m.entrySet())
    {
        if (position > entry.getKey()) { return entry.getValue().toString(); }
    }
}