“尝试做2 ^变量[duplicate]时可能会从double转换为int”>

问题描述 投票:-2回答:2
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);

当我不尝试将任何内容从双精度转换为...时,我在最后一行收到错误“可能从双精度转换为整数”(错误指针指向数字“ 2”)。

java type-conversion syntax-error double
2个回答
1
投票

Math.pow()方法返回一个双精度值,因此,如果需要将其分配给祖先,则需要对其进行强制转换。

   ancestors =(int) Math.pow (2, generations); 

1
投票

使用以下任何一种:

© www.soinside.com 2019 - 2024. All rights reserved.