生活随笔
收集整理的這篇文章主要介紹了
7-2 jmu-Java-01入门-取数字 (2 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本題目要求讀入若干個代表整數的字符串,然后將其轉化為整數。
如果該數>=10000且<=20000,則依次輸出其對應的二進制字符串、八進制字符串、十六進制字符串。
否則將字符串中的每個數字抽取出來,然后將所有數字加總求和。
Integer可以去網上搜搜教程學習一下
輸入樣例:
123
10000
-123
314159265
輸出樣例:
1 2 3 6
10011100010000,23420,2710
1 2 3 6
3 1 4 1 5 9 2 6 5 36
import java
.util
.Scanner
;public class Main {public static void main(String
[] args
) {Scanner sc
=new Scanner(System
.in
);while (sc
.hasNextLine()){String s
=sc
.nextLine();int a
=Integer
.parseInt(s
);if (a
>=10000&&a
<=20000){System
.out
.println(Integer
.toString(a
,2)+","+Integer
.toString(a
,8)+","+Integer
.toString(a
,16));}else{int sum
=0;for (int i
=0;i
<s
.length();i
++){char c
=s
.charAt(i
);if (c
!='-'){int b
=c
-'0';sum
+=b
;System
.out
.print(c
+" ");}}System
.out
.println(sum
);}}}
}
import java
.util
.Scanner
;public class Main {public static void main(String
[] args
) {Scanner sc
=new Scanner(System
.in
);while (sc
.hasNextLine()){String s
=sc
.nextLine();if (s
.equals(""))continue;int a
=Integer
.parseInt(s
);if (a
>=10000&&a
<=20000){System
.out
.println(Integer
.toString(a
,2)+","+Integer
.toString(a
,8)+","+Integer
.toString(a
,16));}else{s
=Integer
.toString(Math
.abs(a
));int sum
=0;for (int i
=0;i
<s
.length();i
++){char c
=s
.charAt(i
);int b
=c
-'0';sum
+=b
;System
.out
.print(c
+" ");}System
.out
.println(sum
);}}}
}
總結
以上是生活随笔為你收集整理的7-2 jmu-Java-01入门-取数字 (2 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。