3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
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貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
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!");
}
最后它工作了,代碼通過(guò)了所有測(cè)試

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
我認(rèn)為問(wèn)題在于,要求明確規(guī)定你要編寫一個(gè)名為接受對(duì)象的方法。您已經(jīng)編寫了一個(gè)方法,然后編寫了一個(gè)正在接受 的方法。inputBirthdayScannermaininputBirthdayString, int, int
將代碼從方法移動(dòng)到方法,刪除掃描儀實(shí)例化,并修改方法以接受掃描儀(可能是 .maininputBirthdayinputBirthdayinputBirthday(Scanner abc)
編寫的代碼在 intellij 中工作,因?yàn)樗且粋€(gè)完整的程序。但對(duì)于網(wǎng)站,他們希望有一個(gè)特定的方法簽名。這種方法與此類在線代碼位置所期望的沒(méi)有什么不同。leetcode
OP進(jìn)行了編輯,但同樣,要求規(guī)定該方法應(yīng)如下所示:
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!");
}
所以,再次:
獲取方法簽名以匹配要求(不清楚它是否應(yīng)該是靜態(tài)的,因此可能需要是)。public static inputBirthday(Scanner abc)
不要在方法中實(shí)例化掃描儀。inputBirthday
要從 IDE 進(jìn)行測(cè)試,請(qǐng)執(zhí)行以下操作:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// either instantiate the enclosing class, or make inputBirthday static
inputBirthday(in);
}
添加回答
舉報(bào)