题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
package cn.ls.lanqiao;
import java.util.*;
public class Test24 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
converse(n);
/*
* for (int i = n.length() - 1; i >= 0; i--) { System.out.print(n.charAt(i)); }
*/
}
public static void converse(int n) {
String s = Integer.toString(n);
System.out.println(n + "是" + s.length() + "位数");
char[] ch = s.toCharArray();
for (int i = ch.length - 1; i >= 0; i--) {
System.out.print(ch[i]);
}
}
}
本文介绍了一个简单的Java程序,该程序接收一个不超过五位的正整数作为输入,然后输出该数字的位数及其逆序形式。通过将整数转换为字符串,可以轻松地获取其长度并反转字符数组来实现逆序打印。
5万+

被折叠的 条评论
为什么被折叠?



