2013-03-04 128 views
0

我有麻煩轉換字符串(birthyear)成int(年齡)。我希望有人輸入他們的出生年份,並讓程序做一個簡單的減法計算來計算他們的年齡。我是編程新手,所以我一直在尋找,大多數地方告訴我同樣的事情。二元運算符的不良操作數類型「 - 」第一種類型:int;第二種類型:java.lang.String

Integer.parseInt(birthyear); 

不過,話說這樣做,當我嘗試做數學題......

int age = year-birthyear; 

我得到錯誤的稱號。

public class WordGameScanner 
{ 
    public static void main(String[] argus) 
    { 
     String name; 
     String home; 
     String birthyear; 
     String course; 
     String reason; 
     String feedback; 
     int year = 2013; 

     Scanner input = new Scanner(System.in); 
     System.out.print("What is your name: "); 
     name = input.nextLine(); 
     System.out.print("Where are you from: "); 
     home = input.nextLine(); 
     System.out.print("What year were you born: "); 
     birthyear = input.nextLine(); 
     Integer.parseInt(birthyear); 
     System.out.print("What are you studying: "); 
     course = input.nextLine(); 
     System.out.print("Why are you studying " + course + ": "); 
     reason = input.nextLine(); 
     System.out.print("How is " + course + " coming along so far: "); 
     feedback = input.nextLine(); 

     int age = year-birthyear; 

     System.out.println("There once was a person named " + name + 
      " who came all the way from " + home + 
      " to study for the " + course + 
      " degree at --------------.\n\n" + name + 
      " was born in " + birthyear + " and so will turn " + age + 
      " this year."); 
     System.out.println(name + " decided to study the unit ------------, because \"" + 
      reason + "\". So far, ----------- is turning out to be " + 
      feedback + "."); 
    } 
} 

對不起,如果這是在錯誤的地方,這只是我的第二篇文章在這裏。我只是點擊「問一個問題」,然後按照指示>。 <

+1

你只是丟棄()''用整數#parseInt函數的返回值。不要這樣做。 – 2013-03-04 23:13:09

+0

快速問題:您是如何將代碼發佈到所有縮進問題的?當我嘗試使用「代碼塊」時,它完全沒有縮進地發佈它。 – Mav 2013-03-04 23:16:47

+0

只需按下「Tab」,我就可以使用[Chrome擴展讓我縮進代碼塊](https://chrome.google.com/webstore/detail/textarea-code-formatter/dgkbnngeehclnibbdblaimapibmmcdal),但您應該沒有問題通過粘貼代碼獲得相同的結果,在編輯器中突出顯示它,然後按下「代碼」按鈕。 – 2013-03-04 23:42:24

回答

7
int age = year-Integer.parseInt(birthyear); 

調用parseInt不重新定義變量String birthYear作爲int,它只是返回一個int值,可以在另一變量中存儲(如int birthYearInt = Integer.parseInt(birthYear);)或通過表達式如上述使用。


您可能還想花一點時間思考輸入。

您的用戶可能只輸入最後兩位數字(「83」,而不是「1983」),那麼你可以這樣做:

int birthYearInt = Integer.parseInt(birthYear); 
if (birthYear.length() == 2) { 
    // One way to adjust 2-digit year to 1900. 
    // Problem: There might not be more users born in 1900 than born in 2000. 
    birthYearInt = birthYearInt + 1900; 
} 
int age = year = birthYearInt; 

或者,你可以使用java.text.NumberFormat妥善處理逗號輸入。 NumberFormat是處理來自人類的數字的好方法,因爲它處理人們將數字格式與計算機格式不同的方式。


另一個問題是,這裏使用了中國時代的編號系統,其中在新年每個人的年齡增加一(中國農曆的,而不是公曆)。這不是他們年齡的計算方式,儘管世界各地。例如,在美國和歐洲的大部分地區,您的年齡在您出生的週年紀念日都會增加。

2
Integer.parseInt(birthyear); 

不會覆蓋birthyear值,不它的類型從字符串更改爲int

,所以你要做的

int intbirthyear = Integer.parseInt(birthyear); 

然後

int age = year-intbirthyear; 
+0

啊!我忘了給它一個變量! – Mav 2013-03-04 23:15:17

0

Integer.parseInt不會改變birthyear從一個String到一個int,它只是返回該字符串表示的int。您需要將此返回的值分配給另一個變量並在減法中使用。

0

即使在執行Integer.parseInt(birthyear);後沒有分配任何東西, birthyear將是一個字符串。

要麼使用 int age = year-Integer.parseInt(birthyear);

OR

int ibirthyear = Integer.parseInt(birthyear); 
int age = year - ibirthyear; 
相關問題