第1个回答 2010-12-20
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws Exception{
int sum=0,temp;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(" 请输入: ");
String s=br.ReadLine();
for(int i=0;i<s.length();i++){
temp=s.CharAt(i);
if(temp>=0&&temp<=10){sum=sum+temp;}
else{continue;}
}
System.out.println("The sum is:"+sum);
}
}
第2个回答 2010-12-20
import java.io.*;
public class Test_2 {
public static void main(String[] args) throws Exception{
int sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(" 请输入: ");
String s=br.readLine();
for(int i=0;i<s.length();i++){
char t=s.charAt(i);
String str=String.valueOf(t);
if(str.matches("[0-9]"))
{
sum=Integer.parseInt(str);
sum+=sum;
}
}
System.out.println("The sum is:"+sum);
}
}
第3个回答 2010-12-20
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class tst {
public static void main(String[] args) {
String s = "fas30deg60ds190$%^";
Matcher m = Pattern.compile("\\d+").matcher(s);
int num = 0;
while (m.find()) {
num = num + Integer.parseInt(m.group(0));
}
System.out.println(num);
}
}本回答被提问者采纳