2014-04-29 32 views
-2

所以我有這樣的:無法複製字符串和int

(香蕉*蘋果);

,我得到這個錯誤:

不聲明 不好操作類型爲二進制運算符「」 第一種類型:字符串 第二類型:int *

這是如此簡單,但我只是忘記。

+0

是錯誤,它說是正確的,你不能乘串和int。您需要使用'Integer.parseInt()'將字符串轉換爲整數,然後才能進行乘法運算。 –

+0

@PradeepSimha - 謝謝你 – user3561829

+0

請問您想闡述一下你的代碼一點點? 像什麼是類型的香蕉和蘋果,怎麼在那裏他們開始等.. 這個問題真的是沒有答案,因爲如果你沒有申報香蕉爲String和蘋果作爲一個int,那麼你不能將它們相乘 – ThaBomb

回答

0

也許你不喜歡的東西:

String banana = "3"; 
int apple = 2; 

int result = banana*apple; 

只需使用Integer.parseInt()

int result = Integer.parseInt(banana)*apple; 
+0

yess謝謝。其實我沒有String banana =(userInput.getText());和apple =(userInteger + userInteger2);但耶 – user3561829

3
String banana = "2"; 
int apple = 1; 

int result = (Integer.valueOf(banana))*apple; 

System.out.println("Result= " + result); 

試一下;)

+0

謝謝我得到它 – user3561829

+0

我沒有使用parseInt,因爲這種方式你有2個答案,並與parseInt你需要封裝一個嘗試和捕獲 – DavideBar

+0

哦好吧謝謝 – user3561829