我试图编写一个接收当前日期输入的程序,然后显示明天的日期。 我认为它几乎完成了,但是当我将当前日期变量测试到maxDays时出现问题。我永远不会明天得到正确的日期。
我认为代码的问题在于这一部分。谢谢你看看!
if (((presentDay > maxDays || presentDay < 1)) || (presentMonth > 12)
|| (presentYear < 0)) {
JOptionPane.showMessageDialog(null, "Please enter valid date \n\n"
+ "consider number of days in each month\n"
+ "entries must be greater than or equal to 1");
} else {
if (presentDay == maxDays) {
if (presentMonth == 12) {
tmrwDay = 1;
tmrwMonth = 1;
tmrwYear = presentYear++;
} else {
tmrwDay = 1;
tmrwMonth = presentMonth++;
tmrwYear = presentYear;
}
} else {
tmrwDay = presentDay++;
tmrwMonth = presentMonth;
tmrwYear = presentYear;
}
displayTmrwDate(tmrwDay, tmrwMonth, tmrwYear);
}
这是完整的程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab5 extends JFrame implements ActionListener {
private JTextField dayTextField, monthTextField, yearTextField;
private JLabel nextDateCalculatorLabel, presentDayLabel,
presentMonthLabel, presentYearLabel;
public static void main(String[] args) {
Lab5 frame = new Lab5();
frame.pack();
frame.setSize(300, 200);
frame.createGUI();
frame.setLocationRelativeTo(null); //centers the frame
frame.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
nextDateCalculatorLabel = new JLabel("- - - - -" + " "
+ "N e x t" + " " + "D a y" + " " + "C a l c u l a t o r"
+ " " + "- - - - -");
window.add(nextDateCalculatorLabel);
presentDayLabel = new JLabel("Enter Present Day:");
window.add(presentDayLabel);
dayTextField = new JTextField(5);
window.add(dayTextField);
dayTextField.addActionListener(this);
presentMonthLabel = new JLabel("Enter Present Month:");
window.add(presentMonthLabel);
monthTextField = new JTextField(5);
window.add(monthTextField);
monthTextField.addActionListener(this);
presentYearLabel = new JLabel("Enter Present Year:");
window.add(presentYearLabel);
yearTextField = new JTextField(5);
window.add(yearTextField);
yearTextField.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent event) {
int presentDay, presentMonth, presentYear;
int maxDays, tmrwDay, tmrwMonth, tmrwYear;
String inputString;
inputString = dayTextField.getText();
presentDay = Integer.parseInt(inputString);
inputString = monthTextField.getText();
presentMonth = Integer.parseInt(inputString);
inputString = yearTextField.getText();
presentYear = Integer.parseInt(inputString);
maxDays = calcMaxDays(presentMonth, presentYear);
if (((presentDay > maxDays || presentDay < 1)) || (presentMonth > 12)
|| (presentYear < 0)) {
JOptionPane.showMessageDialog(null, "Please enter valid date \n\n"
+ "consider number of days in each month\n"
+ "entries must be greater than or equal to 1");
} else {
if (presentDay == maxDays) {
if (presentMonth == 12) {
tmrwDay = 1;
tmrwMonth = 1;
tmrwYear = presentYear++;
} else {
tmrwDay = 1;
tmrwMonth = presentMonth++;
tmrwYear = presentYear;
}
} else {
tmrwDay = presentDay++;
tmrwMonth = presentMonth;
tmrwYear = presentYear;
}
displayTmrwDate(tmrwDay, tmrwMonth, tmrwYear);
}
}
private int calcMaxDays(int m, int y) {
int maxDays = 0;
switch (m) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
maxDays = 31;
break;
case 4: case 6: case 9: case 11:
maxDays = 30;
break;
case 2:
if ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)) {
maxDays = 29;
} else {
maxDays = 28;
}
break;
}
return maxDays;
}
private static void displayTmrwDate(int tD, int tM, int tY) {
JOptionPane.showMessageDialog(null, "The Date Tomorrow is:\n"
+ tD + "/" + tM + "/" + tY);
}
}
答案 0 :(得分:2)
您使用了后增量presentDay++
,并且需要在if语句的每个语句中使用预增量++presentDay
。
例如在你的陈述中:
tmrwDay = presentDay++;
首先,presentDay
的值已分配给tmrwDay
,然后presentDay
的值会递增。如果您要预先增加++presentDay
,则presentDay
的值会增加,然后将其值分配给tmrwDay
。