2023 Java 实验Lab1-task1

 

Given an input string of a signed 32-bit integer x from the console, print out x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31-1], then print out 0.
输入一个32位以内的string类值,输出它的相反值,如果相反值超出范围,则输出0

Example#1:

Input:

123

Output:

321

Example#2:

Input:

-256

Output:

-652

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		long input = sc.nextLong();//一定要用long,int的位数太少了,位数多了的话会报错
		if(input>=Integer.MAX_VALUE||input<=Integer.MIN_VALUE)//判断是否高于整型变量的最大值
		{
			System.out.print("0");
			return;
		}
        if(input>=0)/此时是正数
		{
		while(input!=0)
		{
			long reverse =0;
			reverse=input%10;
			input = input/10;
			System.out.print(reverse);
		}
		}
		else//此时是负数
		{
			long abinput=Math.abs(input);
			System.out.print("-");
			while(abinput!=0)
			{
				long reverse =0;
				reverse=abinput%10;
				abinput = abinput/10;
				System.out.print(reverse);
			}
		}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值