2014-11-14 120 views
-4
System.out.println("Aside from that how old are, because I'm 15."); 
String age = input.nextLine(); 

下一行有問題,但不知道爲什麼在netbeans上。我認爲它必須採取一些措施。If if語句有問題

if(age > 20){ 
System.out.println("Were not really the same age."); 
} 
+2

H ow可以一個字符串高於20嗎?改用'input.nextInt()'。 – 2014-11-14 23:53:55

回答

-1

變化:

String age = input.nextLine(); 

到:

int age = input.nextInt(); 
2

你是一個String對象比較爲整數。 首先將字符串轉換爲整數,然後比較整數。 爲此,請使用Integer.parseInt(age)。這將返回一個等於 的整數到字符串中寫入的數字。

String age = input.nextLine(); 
int ageInteger = Integer.parseInt(age); 
if (ageInteger > 20) { 
    System.out.println("..."); 
} 
0

你可以這樣做2種方式

  1. 您可以將字符串轉換爲整數:
System.out.println("Aside from that how old are, because I'm 15."); 
    String age = input.nextLine(); 
    int ageInInt = Integer.parseInt(age); 
    if(ageInInt > 20){ 
     System.out.println("Were not really the same age."); 
    } 

或者2.你可以從輸入讀取中:

System.out.println("Aside from that how old are, because I'm 15."); 
int ageInInt = input.nextInt(); 
if(ageInInt > 20){ 
    System.out.println("Were not really the same age."); 
}