编写一个称为inputBirthday的方法,该方法接受控制台的“扫描仪”作为参数,并提示用户输入出生的月,日和年,然后以适当的格式打印出生日期。这是与用户对话的示例:
我需要这样输入-
您是哪个月出生的? 8
您出生的月份的名字是什么?五月
你是哪一年出生的? 1981
输出应该像这样-
您出生于1981年5月8日。您年纪很大!
public static void main(String[] args) {
inputBirthday();
}
public static void inputBirthday() {
Scanner abc = new Scanner(System.in);
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!");
}
答案 0 :(得分:0)
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!");
}
最终可以正常工作,并且代码通过了所有测试
答案 1 :(得分:0)
vmName Packages count maxValue Validation
--------------------------------------
Vm1 a,b,c,d 4 4 passed
vm2 a,b,c 3 4 failed
vm3 a,b 2 4 failed