作者SMUGEN (S‧無限)
看板NTUE-CS102
標題Re: [閒聊] 程設作業
時間Fri Oct 29 02:48:34 2010
那我也來馬後炮,野人獻曝一下
// 縮短打字長度 System.out.println -> println, System.out.print -> print
private static void println(Object obj) { System.out.println(obj); }
private static void print(Object obj) { System.out.print(obj); }
public static void main(String args[]) throws IOException {
...
input = stdin.readLine().split(",");
// 讀入一串字串並用 "," 分割為 String array
nums = new double[input.length];
// 建立與 String array 同 size 的 Double array 以儲存數列
for (int i=0; i < input.length; i++){
try { nums[i] = Double.parseDouble(input[i].trim()); }
// 將字串去頭尾空白後轉型為 Double
catch (NumberFormatException err){ // 格式錯誤,字串並非為數字
println("Wrong input!");
System.exit(0);
}
}
Arrays.sort(nums); // 將陣列從小到大排序
for (int i = nums.length-1; i >= 0; i--){ // 將陣列反轉為從大到小
if (nums[i] % 1 == 0) print( (int)nums[i] );
// 如該數為整數則轉型回整數以整數格式輸出
else print( nums[i] ); // 為小數則保留原型態以小數輸出
if (i != 0) print(',');
}
}
我是覺得用try catch感覺比較Java啦,一個字元一個個去看感覺比較C++一點 @_@
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.61.125
→ SMUGEN:話說sort用內建的會不會被扣分啊...XD 10/29 02:54
推 ann263028:這看起來是不同領域的東西...我們C++學太好了JAVA就...? 10/29 22:53