日历程序错误输出

时间:2014-04-04 16:37:48

标签: java applet

我正在为学校制作日历程序...任务:创建一个包含三个字段(dd,mm,yyyy)和一个生日按钮的Applet ...然后Applet将显示(通过算法字符串和循环)突出显示生日的正确月份,年份和日期...日期必须正确设置...例如... DEC 14第1个必须落在W列下面...程序运行完善!除了...我无法正确返回无效输入...例如,如果我放了13个月,我得到无效的月份输入:13(这是好的)但是如果我放14+我什么都没得到......同样的事情,过去31天......看看错误检查是什么样的,我有数组中的日期和月份......

//HW 10         BirthdayApplet

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
//Applet..
public class BirthdayApplet extends Applet implements ActionListener {
//Constants...
private static final int FIRST_DAY = 3;
//Instance Variables...
private int inputDay, inputMonth, inputYear;
private boolean displayCalendar = false; // flag to display calendar
private int firstDayofInputMonth = 0;
//Array of days in int...
private int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 
    31, 30, 31, 30, 31};
//Array of months in string...
private String monthNames[] = {"", "January", "February", "March",
    "April", "May", "June", "July", "August", "September", "October",
    "November", "December"};
//Labels...
private Label monthLabel;
private Label dayLabel;
private Label yearLabel;
//TextFields...
private TextField monthField;
private TextField dayField;
private TextField yearField;
//Button...
private Button displayButton;
//Init...
public void init(){
//Creation of Labels...        
    monthLabel = new Label("Month (mm):");
    dayLabel = new Label("Day (dd):");
    yearLabel = new Label("Year (yyyy):");
//Creation of Fields...
    monthField = new TextField (10);
    dayField = new TextField (10);
    yearField = new TextField (10);
//Creations of Button...
    displayButton = new Button("Display Birthday");
    displayButton.addActionListener(this);
//Addition of all created to applet...        
    add(monthLabel);
    add(monthField);
    add(dayLabel);
    add(dayField);
    add(yearLabel);
    add(yearField);
    add(displayButton);
//Set window size...      
    setSize(1000,1000);
}
//Action...    
public void actionPerformed (ActionEvent e){
    if (e.getSource() == displayButton){
        displayCalendar = true;
//Convert input strings to int...            
        inputMonth = Integer.parseInt(monthField.getText());
        inputDay = Integer.parseInt(dayField.getText());
        inputYear = Integer.parseInt(yearField.getText());
//First day of month getter...               
        firstDayofInputMonth = getDayOfWeek(getDay(inputMonth, 1));
    }
    repaint();//Repaint on new action...
}
//Paint...   
public void paint (Graphics g){
    Font Default = new Font("TimesRoman", Font.ROMAN_BASELINE, 20);//Set Font....
    Font BOLD = new Font("TimesRoman", Font.BOLD, 23);
    g.setFont(Default);
        int xCoord = 450;
        int yCoord = 100;
        int Counter=0;//Value traversing through loop....
        if (displayCalendar == true){
//Month/Day Validation...        
            if(inputMonth>12 || inputMonth<1)
                g.drawString("INVALID Month input: "+inputMonth , 520, 75);            
            else if(inputDay>31|| inputDay<1)
                g.drawString("INVALID Day input: "+inputDay , 520, 100);
//If good data...        
            else{

帮助?

1 个答案:

答案 0 :(得分:0)

你的逻辑错误。如果您确定inputDay无效,则无论else是否失效,inputMonth都会生效。

inputDay条件设为else if,以便else条件仅在两个条件均为false(已验证)时运行。

if(inputMonth>12 || inputMonth<1)
        g.drawString("INVALID Month input: "+inputMonth , 520, 75);            
else if(inputDay>31|| inputDay<1)
        g.drawString("INVALID Day input: "+inputDay , 520, 100);
//If good data...        
else{

当然,在正常情况下,您需要添加更好的验证,例如11月31日,2月29日(闰日除外)等。