Java根据正整数的位数并逆序打印出各位数字
程序员文章站
2024-03-05 19:14:49
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
程序设计:
import java.util.scanner;
public...
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
程序设计:
import java.util.scanner; public class ex24 { public static void main(string[] args) { ex24 tn = new ex24(); scanner s = new scanner(system.in); long a = s.nextlong(); if(a < 0 || a > 100000) { system.out.println("error input, please run this program again"); system.exit(0); } if(a >=0 && a <=9) { system.out.println( a + "是一位数"); system.out.println("按逆序输出是" + '\n' + a); } else if(a >= 10 && a <= 99) { system.out.println(a + "是二位数"); system.out.println("按逆序输出是" ); tn.converse(a); } else if(a >= 100 && a <= 999) { system.out.println(a + "是三位数"); system.out.println("按逆序输出是" ); tn.converse(a); } else if(a >= 1000 && a <= 9999) { system.out.println(a + "是四位数"); system.out.println("按逆序输出是" ); tn.converse(a); } else if(a >= 10000 && a <= 99999) { system.out.println(a + "是五位数"); system.out.println("按逆序输出是" ); tn.converse(a); } } public void converse(long l) { string s = long.tostring(l); char[] ch = s.tochararray(); for(int i=ch.length-1; i>=0; i--) { system.out.print(ch[i]); } } }