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);
}
添加回答
舉報