我正在尝试编写以下代码.但它给了我错误,请帮助我.
int six=06;
int seven=07;
int abc=018;
int nine=011;
System.out.println("Octal 011 ="+nine);
System.out.println("octal O18 =" + abc);
Run Code Online (Sandbox Code Playgroud)
为什么我不能给018和019变量.我可以给变量赋值020和021.为什么会这样?这背后的原因是什么,请告诉我.
我得到了以下错误
integer number too large: 018
int eight=018;
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 21
为什么我不能给018和019变量.
因为前缀的整数文字0被视为八进制,而'8'和'9'不是有效的八进制数字.
八进制数字由ASCII数字0后跟一个或多个散布有下划线的ASCII数字0到7组成,并且可以表示正整数,零整数或负整数.
试图在八进制数中使用'8'就像尝试在十六进制中使用'G'一样......它很简单,不是该基数中使用的符号集的一部分.
Kan*_*mar 17
// 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;
Run Code Online (Sandbox Code Playgroud)
如果数字是字符串格式,那么您可以使用下面的方法将其转换为int
String text = "0b111101111";
int value = text.toLowerCase().startsWith("0b") ? Integer.parseInt(text.substring(2), 2)
: Integer.decode(text);
Run Code Online (Sandbox Code Playgroud)
前缀0表示八进制(8基)(数字0-7).
public class MainClass{
public static void main(String[] argv){
int intValue = 034; // 28 in decimal
int six = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9
System.out.println("Octal 010 = " + eight);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42498 次 |
| 最近记录: |