新手村 简单字符串 数字反转(升级版)

题目链接

题意理解

这题只要你铁下心来用Java,那么就是随便写,注意一下取值范围,是有20位的,会爆int的精度,所以我用的是Long。

代码

import java.util.Scanner;

public class Main {
    private static long reverseInt(String s) {
        return Long.valueOf(new StringBuilder(s).reverse().toString());
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();

        if(string.endsWith("%")) {
            String beforeReverse = string.substring(0, string.length() - 1);
            System.out.println(reverseInt(beforeReverse) + "%");
        } else if(string.contains("/")) {
            String[] divides = string.split("/");
            System.out.println(reverseInt(divides[0]) + "/" + reverseInt(divides[1]));
        } else if(string.contains(".")) {
            String[] nums = string.split("\\.");
            String temp = reverseInt(nums[0]) + "." + reverseInt(nums[1]);
            if(temp.endsWith(".0")) {
                System.out.println(temp);
            } else {
                while(temp.endsWith("0")) {
                    temp = temp.substring(0, temp.length() - 1);
                }
                System.out.println(temp);
            }
        } else {
            System.out.println(reverseInt(string));
        }
        scanner.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值