[英]Error When Printing a Large Number, Min and Sec Become Negative
I am getting an error when printing a large number, minutes and seconds become negative. 打印大量数字时出现错误,分钟和秒变成负数。 I think it is due to the size of the number that is printing. 我认为这是由于要打印的数字的大小。
import java.util.Scanner;
public class Minutes
{
public static void main(String[] args)
{
//User information
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number of hours, days, weeks or years: ");
int s = in.nextInt();
//Compute input
int hours = s * 60;
int days = s * 1440;
int weeks = s * 10080;
int years = s * 525600;
//Print results
System.out.println("------------------------------------------------");
System.out.println("Here are your results!");
System.out.println("If you entered hours, this is the number of minutes" + ": " + hours);
System.out.println("If you entered days, this is the number of minutes" + ": " + days);
System.out.println("If you entered weeks, this is the number of minutes" + ": " + weeks);
System.out.println("If you entered years, this is the number of minutes" + ": " + years);
}
}
就是这样,您遇到了integer overflow
,当这种情况发生时,java从Integer.MIN_VALUE
开始计数,为什么我显示负数,将其转换为long
ant应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.