我在Java中編寫代碼來接受一個數字的輸入並輸出它。然後我輸入了一個導致錯誤的大數字。之後,我發現了Java中整數的不同數據類型。 這是我寫的一個例子。Java中的數字數據類型有什麼用途?
import java.util.Scanner;
public class InputOutput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
byte b = scan.nextByte();
short s = scan.nextShort();
int i = scan.nextInt();
long l = scan.nextLong();
System.out.println("The integer is " + b + ".");
System.out.println("The integer is " + s + ".");
System.out.println("The integer is " + i + ".");
System.out.println("The integer is " + l + ".");
}
}
什麼原因存在這麼多的整數數據類型?
因爲它們很有用。一旦你有了更多的Java編程經驗,你將會了解它們的用處。 –
@Zarif Ceaser,你忽略了一個整型,'char'。它與'short',16位一樣大小,但沒有簽名。 –