Java 如何在9999之后继续生成此系列(xyz*3)==zzz
此代码生成0到9999的(xyz+xyz+xyz=zzz系列)。如果我试图超过9999,我会得到一个Java 如何在9999之后继续生成此系列(xyz*3)==zzz,java,numberformatexception,Java,Numberformatexception,此代码生成0到9999的(xyz+xyz+xyz=zzz系列)。如果我试图超过9999,我会得到一个NumberFormatException 代码: //package com.uttara.todo; public class Addition { public static void main(String[] args) { int x=1; int y=1; int z=1; for( x=1;x<99
NumberFormatException
代码:
//package com.uttara.todo;
public class Addition {
public static void main(String[] args) {
int x=1;
int y=1;
int z=1;
for( x=1;x<9999;x++)//this works fine till 999 at 9999 throws numberformat exception
{
for( y=1;y<9999;y++)
{
for( z=1;z<9999;z++)
{
String str=""+x+y+z;
String str1=""+z+z+z;
**int temp=Integer.parseInt(str);//this throws exception**
int temp1=Integer.parseInt(str1);
//temp=temp*3;
//System.out.println(temp);
if((temp*3)==temp1)
System.out.println(temp);
//break;
//x++;
}
}
x++;
}
y++;
//System.out.println("x="+x+"y="+y+"z="+z);
}
}
//package com.uttara.todo;
公共类添加{
公共静态void main(字符串[]args){
int x=1;
int y=1;
intz=1;
对于(x=1;x,每种类型都有其限制,因此Java中的整数介于
-2^31=-2147483648和2^31-1=2147483647
因此,一旦您在该区域之外构建了一个带有数字的字符串,您将收到此异常
您可以选择其他类型。下一个可能使用的类型是Long:
Long.MAX_值=9223372036854775807
Long.MIN_VALUE=-9223372036854775808可能您应该研究不同类型的范围,例如int或Long。提示:这些类型都不能表示任意大的数字。我还想提到biginger:该类后面没有基元类型,因此您必须进行更改,而不仅仅是替换某些类型。因此,parsing BigInteger是在构造函数内部完成的,要将两个BigInteger相乘,需要使用BigInteger.multiply函数,并且3必须是BigInteger。