我的老師給了我這樣一個問題:如何在這個程序中只使用兩個字符串?
編寫一個程序,執行以下操作:
- 輸入你的名字:彼得
- 輸入您的姓氏: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。請幫忙! P.S.他說,只要我不想把它作爲我自己的想法來傳遞,我就可以獲得幫助。
看你的意見,你有可能是*不*想出Bonus2那麼好。只是重複陳述所做的評論就是uselss(該陳述更具可讀性並傳達相同的信息)。 「將int oriyr設置爲2012年的值」將確保您無法獲得積分... – Durandal 2012-02-17 11:40:57