Java 如何更改myDate1的值,并在main方法中打印出新值?
如何使用3个单独的集合方法将myDate1更改为我输入的新日期 下面是三种集合方法Java 如何更改myDate1的值,并在main方法中打印出新值?,java,Java,如何使用3个单独的集合方法将myDate1更改为我输入的新日期 下面是三种集合方法 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
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;
}
public void setMonth(整数mm)
{
月=(月>=1&&mm=1&&isValidDay(日)?日:1);
}
/**赛特年
*@param yyyy年的新值
*将年份设置为yyyy
*/
公共无效设置年(整年)
{
年份=yyy;
}
我想把我写的代码保留在main方法中
下面是代码的其余部分
import java.io.Serializable; // for object I/O to file
import java.util.Scanner;
//public class SimpleDate
public class SimpleDateClientFL implements Serializable
{
private int month;
private int day;
private int year;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int month;
int day;
int year;
SimpleDate myDate1 = new SimpleDate();
SimpleDate myDate2 = new SimpleDate();
System.out.println(myDate1.toString());
System.out.println(myDate1);
myDate1.nextDay();
myDate1.nextDay();
myDate1.nextDay();
System.out.println(myDate1);
System.out.print("enter day: ");
day = input.nextInt();
System.out.print("enter month: ");
month = input.nextInt();
System.out.print("enter year: ");
year = input.nextInt();
System.out.println("my birthday: " + myDate1);
}
/** default constructor
* sets month to 1, day to 1 and year to 2000
*/
public void 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 void 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;
SimpleDateClientFL d1 = (SimpleDateClientFL)d;
if ( month == d1.month
&& day == d1.day
&& year == d1.year )
return true;
else
return false;
}
}
import java.io.Serializable;//用于将对象I/O复制到文件
导入java.util.Scanner;
//公共类简化
公共类SimpleDataClientFL实现可序列化
{
私人整数月;
私人国际日;
私人国际年;
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
整月;
国际日;
国际年;
SimpleDate myDate1=新SimpleDate();
SimpleDate myDate2=新SimpleDate();
System.out.println(myDate1.toString());
System.out.println(myDate1);
myDate1.nextDay();
myDate1.nextDay();
myDate1.nextDay();
System.out.println(myDate1);
系统输出打印(“输入日期:”;
day=input.nextInt();
系统输出打印(“输入月份:”;
month=input.nextInt();
系统输出打印(“输入年份:”;
年份=input.nextInt();
System.out.println(“我的生日:+myDate1”);
}
/**默认构造函数
*将月设置为1,将日设置为1,将年设置为2000
*/
公共void SimpleDate()
{
设定日期(2000年1月1日);
}
/**重载构造函数
*@param mm月初始值
*@param dd天的初始值
*@param yyyy年度初始值
*
*将参数传递给set方法
*/
公共void SimpleDate(整数mm,整数dd,整数yyyy)
{
设定月数(毫米);
设定年份(yyyy);
设定日(dd);
}
/*存取器方法*/
int getMonth(){return month;}
int getDay(){return day;}
int getYear(){返回年份;}
/**变异子法*/
/**设定月
*@param mm月的新值
*如果mm介于1和12之间,则将月份设置为mm
*否则,将月份设置为1
*/
公共无效设置月(整数毫米)
{
月=(月>=1&&mm=1&&isValidDay(日)?日:1);
}
/**赛特年
*@param yyyy年的新值
*将年份设置为yyyy
*/
公共无效设置年(整年)
{
年份=yyy;
}
/**将日期设置为第二天
*/
下一天()
{
如果(!isValidDay(++天))
{
日=1;
如果(++月份>12)
{
月=1;
年份++;
}
}
}
私有布尔值isValidDay(int newDay)
{
int[]daysInMonth={0,31,28,31,
30, 31, 30,
31, 31, 30,
31, 30, 31 };
如果(newDay>daysInMonth[月])
{
如果(月==2&&isLeapYear()&&newDay==29)
返回true;
其他的
返回false;
}
其他的
返回true;
}
私有布尔值isLeapYear()
{
返回!(第%4年!=0
||(年份%100==0和年份%400!=0));
}
/**设定日期
*@param mm月的新值
*@param dd天的新值
*@param yyyy年的新值
*将参数传递给setMonth、setDay和setYear
*/
公共无效设置日期(整数mm、整数dd、整数yyy)
{
setYear(yyyy);//首先设置年份(可以是闰年)
setMonth(mm);//设置下一个月
设定日(dd);//设定日
}
/**托斯特林
*@返回字符串
*以mm/dd/yyyy格式返回日期
*/
公共字符串toString()
{
返回月份+“/”+天+“/”+年;
}
/**相等于
*@param d对象与此对象进行比较
*@如果d等于此对象,则返回true
*否则就错了
*/
公共布尔等于(对象d)
{
if(!(简化的d实例))
返回false;
SimpleDataClientFL d1=(SimpleDataClientFL)d;
如果(月份==d1.1个月)
&&天==d1.5天
&&年份==d1.1年)
返回true;
其他的
返回false;
}
}
提前谢谢。如果我需要提供任何其他信息,请让我知道。您必须知道何时将文件设置为
month = input.nextInt();
您实际上修改了“this”对象,但没有修改“new”下面的对象
因此,当您使用静态main方法时,必须具有对SimpleDate文件的访问权限
很简单,您可以将这些字段公开,这样您就可以像这样修改它:
myDate1.month = input.nextInt();
或者您可以添加一个方法来设置字段,就像您所做的那样!所以你可以称之为
myDate1.setMonth(input.nextInt());
myDate1.setMonth(input.nextInt());