键盘录入一个正整数,把它的各个位上的数字倒着排列形成一个新的整数并输出。 例如:12345 数出54321 78760 输出6787(0省去)

package com.coffn.demos;
/**
 * 4、键盘录入一个正整数,把它的各个位上的数字倒着排列形成一个新的整数并输出。
 例如:12345 数出54321   78760 输出6787(0省去)
 */
import java.util.Scanner;

public class Demo1 {

	public static void main(String[] args) {
		  Scanner sc = new Scanner(System.in);
		  System.out.println("请输入一个正整数:");
		  int num = sc.nextInt();
		  String str = num+"";
		  //反转
		  String reverse =reverse(str);
		  //统计反转后0的个数
		  int count = getSum(reverse);
		  System.out.println();
		  System.out.println("反转后的数字为:"+getString(reverse,count));
	}

	//反转
	public static String reverse(String str) {
		String result = "";
		for (int i = str.length()-1; i >= 0; i--) {
			result += str.charAt(i);
		}
		return result;
	}

	//统计反转之后开头0的个数
	public static int getSum(String str) {
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			if (ch == '0') {
				count++;
			}else {
				break;
			}
		}
		return count;
	}
	
	//消0
	public static String getString(String str, int count ) {
		//用于后面字符串的拼接
		String result = "";
		for (int i = count; i < str.length(); i++) {
			result += str.charAt(i);
		}
		return result;
	}
}

在这里运行后的结果插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值