import java.util.Scanner; public class Test04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = 0 ; while(num<=0){ System.out.println("请输入一个正整数"); num = sc.nextInt(); } String str = String.valueOf(num); int digit = str.length(); System.out.println("位数为:"+digit); // String [] str1 = str.split(""); // String temp = ""; // for (int i = 0; i < str1.length/2; i++) { // temp = str1[i]; // str1[i] = str1[str1.length-1-i]; // str1[str1.length-1-i] = temp; // } // String str2 = String.join("",str1); StringBuffer sb = new StringBuffer(str); str = sb.reverse().toString(); int num1 = Integer.parseInt(str); System.out.println("逆序数为:"+num1); } }
4、【根据输入求输出-求位数、逆序输出】 键盘录入一个正整数,请求:1.它是几位数?2.逆序打印出各位数字。
最新推荐文章于 2023-02-10 20:22:41 发布