3 回答

TA贡献1772条经验 获得超8个赞
public static void inputBirthday(Scanner input) {
Scanner start = new Scanner(System.in);
System.out.print("On what day of the month were you born? ");
int day = input.nextInt();
System.out.print("What is the name of the month in which you were born? ");
String month = input.next();
System.out.print("During what year were you born? ");
int year = input.nextInt();
System.out.println("You were born on " + month + " " + day + "," + " " + year + "." + " You're mighty old!");
}

TA贡献1830条经验 获得超3个赞
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// either instantiate the enclosing class, or make inputBirthday static
inputBirthday(in);
}
public static void inputBirthday(Scanner abc)
{
System.out.print("On what day of the month were you born? ");
int inputDay = abc.nextInt();
System.out.print("What is the name of the month in which you were born? ");
String inputMonth = abc.next();
System.out.print("During what year were you born? ");
int inputYear = abc.nextInt();
System.out.println("You were born on " + inputMonth + " " + inputDay + "," + " " + inputYear + "." + " You're mighty old!");
}
最后它工作了,代码通过了所有测试

TA贡献1836条经验 获得超3个赞
我认为问题在于,要求明确规定你要编写一个名为接受对象的方法。您已经编写了一个方法,然后编写了一个正在接受 的方法。inputBirthdayScannermaininputBirthdayString, int, int
将代码从方法移动到方法,删除扫描仪实例化,并修改方法以接受扫描仪(可能是 .maininputBirthdayinputBirthdayinputBirthday(Scanner abc)
编写的代码在 intellij 中工作,因为它是一个完整的程序。但对于网站,他们希望有一个特定的方法签名。这种方法与此类在线代码位置所期望的没有什么不同。leetcode
OP进行了编辑,但同样,要求规定该方法应如下所示:
public void inputBirthday(Scanner abc) {
System.out.println("On what day of the month were you born? ");
int inputDay = abc.nextInt();
System.out.println("What is the name of the month in which you were born? ");
String inputMonth = abc.next();
System.out.println("During what year were you born? ");
int inputYear = abc.nextInt();
System.out.println("You were born on " + inputMonth + " " + inputDay + "," + " " + inputYear + "." + " You're mighty old!");
}
所以,再次:
获取方法签名以匹配要求(不清楚它是否应该是静态的,因此可能需要是)。public static inputBirthday(Scanner abc)
不要在方法中实例化扫描仪。inputBirthday
要从 IDE 进行测试,请执行以下操作:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// either instantiate the enclosing class, or make inputBirthday static
inputBirthday(in);
}
添加回答
举报