欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

不用数组输出不大于五位数的逆序

程序员文章站 2022-03-21 22:18:37
...

还在学习期,有个作业是这样的,自己敲出来的,求大佬们改进。

public class DaoXu{
	public static void main(String[] args){
		java.util.Scanner s = new java.util.Scanner(System.in);
		System.out.print("请输入一个不多于五位的正整数:");
		int i = s.nextInt();
		if (i >100000 || i < 0){
			System.out.println("非法数字");
			return;
		}
		int weiShu = 0;
		int geWei = 0;
		int shiWei = 0;
		int baiWei = 0;
		int qianWei = 0;
		int wanWei = 0;
		int x = 1;
		for (int m = 1;m <=10000 ;m *= 10 ){
			weiShu += 1;
			if (i / m < 10){
				break;
			}
		}
		if (weiShu == 1){
			System.out.println("这是一个一位数");
			return;
		}
		for (int d = 1 ;d < weiShu ;d++ ){
			x *= 10;
		}
		System.out.println(i + "是" + weiShu + "位数");
		for (int m = 10;m <= x * 10 ;m *= 10 ){
			switch (m){
			case 10:
				geWei = i % m;
				break;
			case 100:
				shiWei = ((i % m) - geWei) / 10;
				break;
			case 1000:
				baiWei = ((i % m) - (shiWei * 10) - geWei) / 100;
				break;
			case 10000:
				qianWei =((i % m) - (baiWei * 100) - (shiWei * 10) - geWei) / 1000;
				break;
			case 100000:
				wanWei = ((i % m) - (qianWei * 1000) - (baiWei * 100) - (shiWei * 10) - geWei) / 10000;
				break;
			}
		}
		String str = "???";
		switch (x){
		case 10:
			str = geWei + "" + shiWei;
			break;
		case 100:
			str = geWei + "" + shiWei+ "" + baiWei;
			break;
		case 1000:
			str = geWei + "" + shiWei + "" + baiWei + "" + qianWei;
			break;
		case 10000:
			str = geWei + "" + shiWei + "" + baiWei + "" + qianWei + "" + wanWei;
			break;
		}
		System.out.println(str);
	}
}
相关标签: java