Java &引用;从double到int的可能有损转换;尝试执行2^变量时
我在最后一行(错误指针指向数字“2”)接收到错误“从双精度到int的可能有损转换”,但我没有尝试将任何东西从双精度转换为int。我缺少什么Java &引用;从double到int的可能有损转换;尝试执行2^变量时,java,type-conversion,syntax-error,double,Java,Type Conversion,Syntax Error,Double,我在最后一行(错误指针指向数字“2”)接收到错误“从双精度到int的可能有损转换”,但我没有尝试将任何东西从双精度转换为int。我缺少什么 System.out.println("This is the ancestry calculator. Enter number of generations: "); Scanner input = new Scanner(System.in); int generations = input.nextInt(); int ancestors; ance
System.out.println("This is the ancestry calculator. Enter number of generations: ");
Scanner input = new Scanner(System.in);
int generations = input.nextInt();
int ancestors;
ancestors = Math.pow (2, generations);
方法返回一个双精度值,因此如果需要将其分配给祖先,则需要对其进行强制转换
ancestors =(int) Math.pow (2, generations);
使用以下任一项:
double ancestors = Math.pow (2.0, generations)
或
祖先
是一个int
,而Math.pow
返回一个双祖先=1
int ancestors = (int) Math.pow (2, generations)