对于这个简单的程序,我们有2个类可以使用MyDate和DueDates(当然是main类)。在初始化MyDate类的对象数组之后private MyDate [] dueDates = new MyDate[10],一个特定的函数要求我们“从传入的扫描器中读取数组中日期的值”。我很困惑,因为我 - 一个弱的程序员 - 只用于Scanner类似的东西System.in。这不是我不理解的; 我怎么能读到这些价值观?因为我无法使用input.next()或不能使用类型Class的数组。public class DueDates {
private MyDate[] dueDates ;
public DueDates() {
//***** write the code for this method here
dueDates = new MyDate [10];
}
//set array to have size of parameter passed in
public DueDates(int max) {
//***** write the code for this method here
dueDates = new MyDate[max];
}
/*reads in values for the dates in the array from the Scanner passed in ------having issues here*/
public boolean inputDueDates(Scanner in) {
//***** write the code for this method here
in = new Scanner(System.in);
dueDates = in. // I'm lost at this point
return true;
}}public class MyDate {
private int day = 1;
private int month = 1;
private int year = 2018;
public MyDate() {
}
public String toString() {
return new String ("" + year + "/" + month + "/" + day);
}
public boolean inputDate(Scanner in) {
month = 0;
day = 0;
year = 0;
do {
System.out.print ("Enter month - between 1 and 12: ");
if (in.hasNextInt())
this.month = in.nextInt();
else {
System.out.println ("Invalid month input");
in.next();
}
} while (this.month <= 0 || this.month > 12);
do {
System.out.print ("Enter day - between 1 and 31: ");
if (in.hasNextInt())
this.day = in.nextInt();
else {
System.out.println ("Invalid day input");
in.next();
}
}
2 回答
喵喔喔
TA贡献1735条经验 获得超5个赞
我将假设MyDate构造函数是: Date(int day, int month, int year){...}
现在您需要初始化10个日期,因此您可以使用for循环:
for(int i=0;i<10;i++){ int day = in.nextInt(); int month = in.nextInt(); int year = in.nextInt(); dueDates[i]=new MyDate(day, month, year); }
添加回答
举报
0/150
提交
取消