2015-10-01 22 views
-3

我需要在java(netbeans)中用戶插入任何包含字母和數字的序列(例如:aa5bgd6dcx78)的程序,然後我需要總結數字,如5 + 6 + 7 + 8並打印結果。任何想法如何我可以做到這一點?如何計算包含在字母數字序列中的數字?

我需要用戶,子字符串和等於。

謝謝

+1

你已經編寫一些代碼? –

+0

嘗試正則表達式來刪除字母和子串來獲得每個數字,然後總和 –

+0

是的,我有String x = JOptionPane.showInputDialog(「寫你的序列」) String o = x.substring(); –

回答

0
String input = "aa5bgd6dcx78"; 

int total = 0; 

for(int i = 0; i < input.length(); i++) { 
    curChar = input.charAt(i); 
    if(curChar.isDigit()) { 
     total += curChar; 
    } 
} 

System.out.println("Total is: " + total); 
相關問題