猿问

BJP4 练习 3.20: 输入出生日期

编写一个名为 inputBirthday 的方法,该方法接受控制台的扫描仪作为参数,并提示用户输入出生月份、日期和年份,然后以合适的格式打印出生日期。下面是与用户的对话示例:

我需要像这样接受这个输入 -

你出生在一个月中的哪一天?8
你出生的月份叫什么名字?五月
你出生在哪一年?1981

输出应该是这样的——

您出生于1981年5月8日。你老了!


慕妹3146593
浏览 172回答 3
3回答

料青山看我应如是

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

牛魔王的故事

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!");    }最后它工作了,代码通过了所有测试

米脂

我认为问题在于,要求明确规定你要编写一个名为接受对象的方法。您已经编写了一个方法,然后编写了一个正在接受 的方法。inputBirthdayScannermaininputBirthdayString, int, int将代码从方法移动到方法,删除扫描仪实例化,并修改方法以接受扫描仪(可能是 .maininputBirthdayinputBirthdayinputBirthday(Scanner abc)编写的代码在 intellij 中工作,因为它是一个完整的程序。但对于网站,他们希望有一个特定的方法签名。这种方法与此类在线代码位置所期望的没有什么不同。leetcodeOP进行了编辑,但同样,要求规定该方法应如下所示: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);}
随时随地看视频慕课网APP

相关分类

Java
我要回答