为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号