⑴ java中由命令行輸入一個數,將該數字反轉,與原數相加後輸出
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner san = new Scanner(System.in);
int number;
String line;
while(true){
try{
System.out.println("請輸入一個數字:");
line = san.nextLine();
number = Integer.valueOf(line);
int len = line.length();
char [] chNums = new char[len];
for(int i = 0 ; i < len; i ++) {
chNums[len - i -1] = line.charAt(i);
}
int opnumber = Integer.valueOf(String.valueOf(chNums));
System.out.println("倒序相加結果為: " + (opnumber + number));
break;
} catch(Exception e) {
System.out.println("格式錯誤,重新輸入");
continue;
}
}
}
}
⑵ 求Java源代碼:用Java實現輸出:輸入一個正整數,將該數的各位左右反轉輸出
不知道這樣寫是不是符合你的要求?
public class Temp {
public static void main(String[] args) {
String str = String.valueOf(12345);
StringBuffer buff = new StringBuffer();
System.out.println(str);
for(int i=str.length()-1;i>=0;i--){
buff.append(str.charAt(i));
}
String retStr = buff.toString();
Integer retInt = Integer.valueOf(retStr);
System.out.println(retInt);
}
}
⑶ java編程怎麼完成任意輸入一個正整數,反序輸出每一位
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("輸入任意正整數:");
StringBuffer sb = new StringBuffer(input.next());
sb.reverse();
System.out.print("逆序為:");
System.out.println(sb);
}