2013-10-23 28 views
-5
import java.util.*; 
public class Test { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     int x, y, sum; 

     Scanner input = new input(system.in); 

     input = parseInt32(); 


    } 

} 

這裏是個例外:如何在Java中兩個數相加編程

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    input cannot be resolved to a type 
    input cannot be resolved to a type 
    system cannot be resolved to a variable 
    The method parseInt32() is undefined for the type Test 

    at Test.main(Test.java:12) 
+0

你在哪裏定義一個名爲parseInt32()方法? – Rami

+0

您試圖運行無法編譯的代碼。永遠不要這樣做。修正編譯器在嘗試運行之前抱怨的編譯錯誤。 –

+1

'int value = 1 + 1' – Craig

回答

1

Scanner input = new input(system.in); 應該 Scanner input = new Scanner(System.in);

它給你一個錯誤,因爲你試圖將你的輸入設置爲一個int轉換。它應該是這樣的,而不是:

x = input.nextInt();

+0

嗯......是和不是。錯誤是因爲'parseInt32'方法沒有被定義。 – Sinkingpoint

+0

我相信他們試圖使用parseInt32作爲轉換器,即使它被定義爲方法,也不能將掃描器對象設置爲方法。也許如果她創建了一個類型並將其設置爲該類型,那麼它可以工作。 – Mercifies

+0

感謝球員的解決方案,這次我說得對! – Justine

2

您需要使用nextInt();所以像:

public static void main(String[] args) { 
    // TODO Auto-generated method stub 


    System.out.println("First number: "); 
    Scanner input = new Scanner(System.in); 

    int x = input.nextInt(); 

    System.out.println("Second number: "); 
    int y = input.nextInt(); 

    int sum = x+y; 

    System.out.println("Result: "+sum); 
} 
+0

作爲@Mercifies的註釋,應該是System.in而不是system.in。 – Sinkingpoint

+0

哦,是的,當然 – Roskvist

+1

謝謝巴迪,我說得對! – Justine

相關問題