我的老师给了我这个问题:
编写一个执行以下操作的程序:
- 输入您的名字:彼得
- 输入您的姓氏:Opunga
- 请输入您的出生年份:1992
- 嗨,Peter Opunga,今年年底您将满 20 岁。
- 奖励1:适当的评论。(10%)
- 奖励 2:在整个程序中只创建 2 个字符串。(10%)
- 奖励 3:正好使用 4 个 System.out.print。(10%)
现在我对 Java 完全陌生。一个多星期前我刚刚被介绍给它,这就是我想出的:
import java.io.*;
public class A1Chuah {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
//Prints instructions for user to enter first name.
System.out.println("Enter first name: ");
//Obtains user input for first name.
String first = in.readLine ();
//Prints instructions for user to enter last name.
System.out.print ("Enter last name: ");
//Obtains user input for last name.
String last = in.readLine();
//Prints instructions for user to enter birth year.
System.out.print ("Enter your birth year: ");
//Obtains user input for birth year.
String year = in.readLine ();
//Converts String year to int useryr
int useryr = Integer.parseInt(year);
//Sets int oriyr to value of 2012
int oriyr = 2012;
//Sets int outyr as oriyr - useryr
int outyr = oriyr - useryr;
//Prints out information.
System.out.println("Hi" + " " + " " + last + "," + " " + "you will be" + " " + outyr + " " + "by the end of this year");
我已经设法完成了奖金 1 和 3,但似乎无法弄清楚奖金 2。请帮助!PS 他说我可以得到帮助,只要我不试图把它当作我自己的想法来传递。