Java编程 使用递归方法将一个整数的各个数位的数字逆置,例如输入1997,输出显示7991。
java编程:用递归法编程将一个整数的各个数位的数字逆置,例如输入1997,输出显示7991~
import java.util.Scanner;
public class Test20 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
System.out.print("请输入一个整数:");
num = scan.nextInt();
System.out.print("逆置后的整数 : ");
reverse(num);
}
//递归方法
public static void reverse(int num){
if(num <= 0){
return;
}
System.out.print(num%10);
reverse(num/10);
}
}
运行测试:
请输入一个整数:1997
逆置后的整数 : 7991
请输入一个整数:1234
逆置后的整数 : 4321
import java.util.Scanner;
public class Demotest {
public static void main(String[] args) {
System.out.println("输出一个你要转换的数字安回车:");
Scanner reader = new Scanner(System.in);
int a=reader.nextInt();
String str = String.valueOf(a);
char[] des = new char[str.length()];
str.getChars(0, str.length(), des, 0);
for (int i = des.length - 1; i >= 0; i--) {
System.out.print((des[i]));
}
}
}
相信你会喜欢我的这个的,哈哈。
public Class Test {
public static void main(String[] args){
int i = 1997;
String str = String.valueOf(i);
char[] des = new char[str.length];
str.getChars(0, 4, des, 0);
for (int i = des.length - 1; i >= 0; i--) {
System.out.print((des[i]));
}
}
}
刚刚写了一个代码,楼主看看噻
import java.util.*;
public class DescSort{
public static void main(String[] args){
System.out.println("请输入数字");
java.util.Scanner in=new java.util.Scanner(System.in);
int str=in.nextInt();
int sum=0;
while(str!=0){
sum*=10;
sum+=str%10;
str/=10;
}
System.out.println(sum);
}
}
使用结果:
前:989540
后:45989
有问题call我
别忘了选我最佳答案啊。
=SUMPRODUCT((0&MID(A1,ROW($1:$15),1))*10^ROW($1:$15))/10
你说的那些俺看不懂,一个公式搞定。
import java.util.Scanner;
public class Test20 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
System.out.print("请输入一个整数:");
num = scan.nextInt();
System.out.print("逆置后的整数 : ");
reverse(num);
}
//递归方法
public static void reverse(int num){
if(num <= 0){
return;
}
System.out.print(num%10);
reverse(num/10);
}
}
运行测试:
请输入一个整数:1997
逆置后的整数 : 7991
请输入一个整数:1234
逆置后的整数 : 4321
import java.util.Scanner;
public class Demotest {
public static void main(String[] args) {
System.out.println("输出一个你要转换的数字安回车:");
Scanner reader = new Scanner(System.in);
int a=reader.nextInt();
String str = String.valueOf(a);
char[] des = new char[str.length()];
str.getChars(0, str.length(), des, 0);
for (int i = des.length - 1; i >= 0; i--) {
System.out.print((des[i]));
}
}
}
相信你会喜欢我的这个的,哈哈。
public Class Test {
public static void main(String[] args){
int i = 1997;
String str = String.valueOf(i);
char[] des = new char[str.length];
str.getChars(0, 4, des, 0);
for (int i = des.length - 1; i >= 0; i--) {
System.out.print((des[i]));
}
}
}