本题目要求读入若干个代表整数的字符串,然后将其转化为整数。
如果该数>=10000且<=20000,则依次输出其对应的二进制字符串、八进制字符串、十六进制字符串。
否则将字符串中的每个数字抽取出来,然后将所有数字加总求和。
提示:参考jdk文档的Integer,
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Demo1 d = new Demo1();
d.flow(sc.nextLine());
}
sc.close();
}
}
class Demo1{
void flow(String s){
int sum = 0;
if (Integer.parseInt(s) >= 10000 && Integer.parseInt(s) <= 20000){
System.out.print(Integer.toBinaryString(Integer.parseInt(s))
+ "," + Integer.toOctalString(Integer.parseInt(s))
+ "," + Integer.toHexString(Integer.parseInt(s)));
}else{
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '-'){
continue;
}
sum += (int) (s.charAt(i) - 48);
System.out.print(s.charAt(i) + " ");
if (i == s.length() - 1){
System.out.print(sum);
}
}
}
System.out.println();
}
}