当我运行我的代码时,我的输出就是这个
---- jGRASP exec:java AgeClientFL
输入出生日:5输入出生月份:5输入出生年份:5出生日期: 5/5/5
今天:1/1/0年龄:0
Michaels诞生:1995年5月7日里程碑:2016年5月7日 里程碑时代:0
---- jGRASP:操作完成。
对于Today
我已经在10月10日进入了,我希望它以10/10/10的形式出现,但它以1/1/0的形式出现。对于第一个age
,我想得到一个5,但得到一个0. Age at Milestone
应该是21而不是0.
我的代码的哪些部分导致了这些不正确的打印输出以及如何解决这些问题?这是我正在使用的代码的第1部分。
import java.text.*;
import java.util.Date;
public class AgeClientFL {
public static void main(String [] args){
int month, day, year, age;
day = UtilsFL.readInt("Enter birth day: ",false); // String mode
month = UtilsFL.readInt("Enter birth month: ",false);
year = UtilsFL.readInt("Enter birth year: ",false);
SimpleDate dateBirth = new SimpleDate(month,day,year);
System.out.println("Birth: " + dateBirth);
day = UtilsFL.readInt("Enter todays day: ",true); // JOptionPane mode
month = UtilsFL.readInt("Enter todays month: ",true);
year = UtilsFL.readInt("Enter todays year: ",true);
SimpleDate dateToday = new SimpleDate(month,day,year);
System.out.println("Today: " + dateToday);
age = UtilsFL.getAge(dateBirth,dateToday);
System.out.println("Age: " + age);
month = 5;
day = 7;
year = 2016;
SimpleDate milestone = new SimpleDate(month,day,year);
month=5;
day=7;
year=1995;
SimpleDate bdMilestone = new SimpleDate(month,day,year);
int mAge;
mAge = UtilsFL.getAge(bdMilestone, milestone);
System.out.println("Michaels Birth: " + bdMilestone);
System.out.println("Milestone: " + milestone);
System.out.println("Age at Milestone: " + mAge);
}
}
这是第二部分。
import java.util.GregorianCalendar;
import java.util.Date;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Calendar;
public class UtilsFL {
public static int readInt(String prompt,boolean guiFlag) {
if (guiFlag==false){
Scanner input = new Scanner(System.in);
int data;
System.out.print(prompt);
data = input.nextInt();
return data;
}
else if (guiFlag==true) {
int data;
data = Integer.parseInt(JOptionPane.showInputDialog(prompt));
}
return 0;
}
public static SimpleDate today() {
Calendar todayCal = Calendar.getInstance();
SimpleDate todayDate = new SimpleDate();
todayDate.setDate(todayCal.get(Calendar.MONTH) + 1,
todayCal.get(Calendar.DATE),
todayCal.get(Calendar.YEAR));
return todayDate;
}
public static int getAge(SimpleDate dateBd) {
int age;
SimpleDate dateToday = today();
age = getAge(dateBd, dateToday);
return age;
}
public static int getAge(SimpleDate dateBd, SimpleDate dateRef) {
Calendar cal = new GregorianCalendar ();
Calendar now = new GregorianCalendar();
int rAge = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)
|| (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
&& cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))))
{
rAge--;
}
return rAge;
}
}
我认为我的代码第二部分底部的public static int getAge
可能存在问题。但我不确定,我无法弄清楚如何解决它。
以下是我以前的一些代码中引用的SimpleDate类的代码。
import java.io.Serializable; // for object I/O to file
//public class SimpleDate
public class SimpleDate implements Serializable
{
private int month;
private int day;
private int year;
/** default constructor
* sets month to 1, day to 1 and year to 2000
*/
public SimpleDate( )
{
setDate( 1, 1, 2000 );
}
/** overloaded constructor
* @param mm initial value for month
* @param dd initial value for day
* @param yyyy initial value for year
*
* passes parameters to set methods
*/
public SimpleDate( int mm, int dd, int yyyy )
{
setMonth( mm );
setYear( yyyy );
setDay( dd );
}
/* accessor methods */
int getMonth( ) { return month; }
int getDay( ) { return day; }
int getYear( ) { return year; }
/** mutator method */
/** setMonth
* @param mm new value for month
* if mm is between 1 and 12, sets month to mm
* otherwise, sets month to 1
*/
public void setMonth( int mm )
{
month = ( mm >= 1 && mm <= 12 ? mm : 1 );
}
/** setDay
* @param dd new value for day
* if dd is legal day for current month, sets day to dd
* otherwise, sets day to 1
*/
public void setDay( int dd )
{
day = ( dd >= 1 && isValidDay( dd ) ? dd : 1 );
}
/** setYear
* @param yyyy new value for year
* sets year to yyyy
*/
public void setYear( int yyyy )
{
year = yyyy;
}
/** sets date to the next day
*/
public void nextDay( )
{
if ( ! isValidDay( ++day ) )
{
day = 1;
if ( ++month > 12 )
{
month = 1;
year++;
}
}
}
private boolean isValidDay( int newDay )
{
int [] daysInMonth = { 0, 31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31 };
if ( newDay > daysInMonth[month] )
{
if ( month == 2 && isLeapYear( ) && newDay == 29 )
return true;
else
return false;
}
else
return true;
}
private boolean isLeapYear( )
{
return !( year % 4 != 0
||( year % 100 == 0 && year % 400 != 0 ) );
}
/** setDate
* @param mm new value for month
* @param dd new value for day
* @param yyyy new value for year
* passes parameters to setMonth, setDay, and setYear
*/
public void setDate( int mm, int dd, int yyyy )
{
setYear( yyyy ); // set year first (could be leap year)
setMonth( mm ); // set month next
setDay( dd ); // set day
}
/** toString
* @return String
* returns date in mm/dd/yyyy format
*/
public String toString( )
{
return month + "/" + day + "/" + year;
}
/** equals
* @param d Object to compare to this object
* @return true if d is equal to this object
* false, otherwise
*/
public boolean equals( Object d )
{
if ( !( d instanceof SimpleDate ) )
return false;
SimpleDate d1 = (SimpleDate)d;
if ( month == d1.month
&& day == d1.day
&& year == d1.year )
return true;
else
return false;
}
}
答案 0 :(得分:1)
在方法UtilsFL#readInt
中,您忘记在GUI模式下返回data
:
public static int readInt(String prompt, boolean guiFlag) {
if (guiFlag == false) {
Scanner input = new Scanner(System.in);
int data;
System.out.print(prompt);
data = input.nextInt();
return data;
} else if (guiFlag == true) {
int data;
data = Integer.parseInt(JOptionPane.showInputDialog(prompt));
// HERE: you need to return data
}
return 0;
}
因此,在GUI模式下,您总是得到0.您可能还想简化== true
,== false
,...