为什么 JVM Return 这个整数?
Why Does The JVM Return This Integer?
我做了一个非常简单的数字舍入应用程序,因为我是一个初学者,可能会有一些错误,需要进行一些调试,我们可以继续下去,但这不是我来这里的原因。每当我输入一个大于值 "int" 容量的整数时,它要么是 returns“-1”,要么是一个非常小的数字(它是负数,离零更远),例如“-572589576”。我想知道 JVM return 为什么以及如何做到这一点。下面列出了我的程序代码:
import java.util.Scanner;
public class NumberRounder {
private static Scanner userInput2;
public static void main(String[] args) {
System.out.println("--- Number Rounder ---");
Scanner userInput = new Scanner(System.in);
System.out.println("The number you enter will round it to the nearest value.");
System.out.print("Enter any decimal number: ");
if (userInput.hasNextDouble()) {
System.out.println("\nCalculating...");
System.out.println("Number Rounding...");
double chosenNumber = userInput.nextDouble();
int roundedNumber = (int) Math.round(chosenNumber);
if (roundedNumber == -1) {
System.out.println("\nAn ERROR occured.");
System.out.println("That number is too large for our servers. Sorry for the inconvenience.");
System.exit(0);
}
System.out.println(chosenNumber + " has been rounded to " + roundedNumber);
} else if (!(userInput.hasNextDouble())) {
System.out.println("\nThat is an illegal response...");
userInput2 = new Scanner(System.in);
System.out.println("\nThe number you enter will round it to the nearest value.");
System.out.print("Enter any decimal number between 1-10: ");
double chosenNumber = userInput2.nextDouble();
int roundedNumber = (int) Math.round(chosenNumber);
System.out.println(chosenNumber + " has been rounded to " + roundedNumber);
} else {
System.out.println("Something wrong happened...");
}
}
}
我已经解决了when it returns "-1" 的问题,但是我仍然不知道如何彻底解决这个问题。这就是为什么我在这里问这个问题:
为什么JVM会return这样的随机数?
您将 double
(64 位)强制转换为 int
,因此结果值被截断并且您看到这些 "random"(尽管它们不是随机的) 值。您应该使用 long
为您的号码提供额外的 space。
Whenever I type in an integer that is larger than the capacity of the value "int"
您希望从中得到什么?异常?
它不会在那里。 Java 只是从 MAX_VALUE 圈到 MIN_VALUE
试一试:
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(Integer.MIN_VALUE);
看看它打印了什么。
PS。那是关于数字符号如何以 int 值的实际二进制格式表示的。
我做了一个非常简单的数字舍入应用程序,因为我是一个初学者,可能会有一些错误,需要进行一些调试,我们可以继续下去,但这不是我来这里的原因。每当我输入一个大于值 "int" 容量的整数时,它要么是 returns“-1”,要么是一个非常小的数字(它是负数,离零更远),例如“-572589576”。我想知道 JVM return 为什么以及如何做到这一点。下面列出了我的程序代码:
import java.util.Scanner;
public class NumberRounder {
private static Scanner userInput2;
public static void main(String[] args) {
System.out.println("--- Number Rounder ---");
Scanner userInput = new Scanner(System.in);
System.out.println("The number you enter will round it to the nearest value.");
System.out.print("Enter any decimal number: ");
if (userInput.hasNextDouble()) {
System.out.println("\nCalculating...");
System.out.println("Number Rounding...");
double chosenNumber = userInput.nextDouble();
int roundedNumber = (int) Math.round(chosenNumber);
if (roundedNumber == -1) {
System.out.println("\nAn ERROR occured.");
System.out.println("That number is too large for our servers. Sorry for the inconvenience.");
System.exit(0);
}
System.out.println(chosenNumber + " has been rounded to " + roundedNumber);
} else if (!(userInput.hasNextDouble())) {
System.out.println("\nThat is an illegal response...");
userInput2 = new Scanner(System.in);
System.out.println("\nThe number you enter will round it to the nearest value.");
System.out.print("Enter any decimal number between 1-10: ");
double chosenNumber = userInput2.nextDouble();
int roundedNumber = (int) Math.round(chosenNumber);
System.out.println(chosenNumber + " has been rounded to " + roundedNumber);
} else {
System.out.println("Something wrong happened...");
}
}
}
我已经解决了when it returns "-1" 的问题,但是我仍然不知道如何彻底解决这个问题。这就是为什么我在这里问这个问题: 为什么JVM会return这样的随机数?
您将 double
(64 位)强制转换为 int
,因此结果值被截断并且您看到这些 "random"(尽管它们不是随机的) 值。您应该使用 long
为您的号码提供额外的 space。
Whenever I type in an integer that is larger than the capacity of the value "int"
您希望从中得到什么?异常?
它不会在那里。 Java 只是从 MAX_VALUE 圈到 MIN_VALUE
试一试:
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(Integer.MIN_VALUE);
看看它打印了什么。
PS。那是关于数字符号如何以 int 值的实际二进制格式表示的。