BJP4 练习 3.20:输入生日
BJP4 Exercise 3.20: inputBirthday
编写一个名为 inputBirthday 的方法,该方法接受控制台的 Scanner 作为参数并提示用户输入出生年月日,然后以合适的格式打印出生日期。这是与用户对话的示例:
我需要这样输入-
On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 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!");
}
我认为问题在于要求明确规定您要编写一个名为 inputBirthday
的 方法 来接受 Scanner
对象。您已经编写了一个 main
方法,然后是一个接受 String, int, int
.
的方法 inputBirthday
将代码从main
方法移动到inputBirthday
方法,摆脱扫描器实例化,并修改inputBirthday
方法接受扫描器(可能inputBirthday(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);
}
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!");
}
终于成功了,代码通过了所有测试
test results
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!");
}
编写一个名为 inputBirthday 的方法,该方法接受控制台的 Scanner 作为参数并提示用户输入出生年月日,然后以合适的格式打印出生日期。这是与用户对话的示例:
我需要这样输入-
On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 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!");
}
我认为问题在于要求明确规定您要编写一个名为 inputBirthday
的 方法 来接受 Scanner
对象。您已经编写了一个 main
方法,然后是一个接受 String, int, int
.
inputBirthday
将代码从main
方法移动到inputBirthday
方法,摆脱扫描器实例化,并修改inputBirthday
方法接受扫描器(可能inputBirthday(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);
}
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!");
}
终于成功了,代码通过了所有测试
test results
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!");
}