亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

BJP4 練習 3.20: 輸入出生日期

BJP4 練習 3.20: 輸入出生日期

慕妹3146593 2022-08-03 16:18:09
編寫一個名為 inputBirthday 的方法,該方法接受控制臺的掃描儀作為參數,并提示用戶輸入出生月份、日期和年份,然后以合適的格式打印出生日期。下面是與用戶的對話示例:我需要像這樣接受這個輸入 -你出生在一個月中的哪一天?8你出生的月份叫什么名字?五月你出生在哪一年?1981輸出應該是這樣的——您出生于1981年5月8日。你老了!
查看完整描述

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!");

}


查看完整回答
反對 回復 2022-08-03
?
牛魔王的故事

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!");

    }

最后它工作了,代碼通過了所有測試


查看完整回答
反對 回復 2022-08-03
?
米脂

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);

}


查看完整回答
反對 回復 2022-08-03
  • 3 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號