⑴ 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);
}