10

次のコードを書き込もうとしていますが、エラーが発生します。親切に助けてください。

    int six=06;
    int seven=07;
    int abc=018;
    int nine=011;
    System.out.println("Octal 011 ="+nine);
    System.out.println("octal O18 =" + abc);

なぜ変数に 018 と 019 を与えることができないのですか。変数に値 020 と 021 を与えることができます。なぜこれが起こるのですか?この背後にある理由は何ですか 親切に教えてください。
次のエラーが発生しました

            integer number too large: 018
            int eight=018;
4

9 に答える 9

34

8 進数は基数 8 の数値システムであるため、数字は 0 から 7 の範囲であり、8 進数では 8 桁 (および 9 も) は使用できません。

于 2013-05-08T06:14:00.170 に答える
27
// Decimal declaration and possible chars are [0-9]
int decimal    =  495;        

// HexaDecimal declaration starts with 0X or 0x and possible chars are [0-9A-Fa-f]
int hexa       =  0X1EF; 

// Octal declaration starts with 0 and possible chars are [0-7] 
int octal      =  0757;  

// Binary representation starts with 0B or 0b and possible chars are [0-1]  
int binary     =  0b111101111; 

数値が文字列形式の場合、以下を使用して int に変換できます

String text = "0b111101111";
int value = text.toLowerCase().startsWith("0b") ? Integer.parseInt(text.substring(2), 2)
                                  : Integer.decode(text);
于 2015-12-21T10:15:05.730 に答える