使用java將一個int數字進行翻轉,例如32輸出2
阿新 • • 發佈:2019-01-11
</pre><pre name="code" class="java">public class Test { public static int numberReverse(int number) { int temp = number; String str = "" + temp; char[] ch = str.toCharArray(); int length = str.length(); int i = 0; while(i < length) { int flag = ch[i]; ch[i] = ch[length-1]; ch[length-1] = (char) flag; ++i; --length; } length = str.length(); int count = 0 ;//記錄前面有多少個零 for(i = 0; i < length; i++) { if(ch[i] == '0') { ++count; } if(ch[i] != '0') { break; } } if(count == 0) { System.out.println(ch); } else { if(length==1) { System.out.println(ch); } for(i = count;i < length;i++) { System.out.print(ch[i]); } } return 0; } public static void main(String[] args) { numberReverse(104); } }