gpt4 book ai didi

java - 为什么java程序使用^运算符时的输出是这样的?

转载 作者:行者123 更新时间:2023-12-01 22:17:52 24 4
gpt4 key购买 nike

我编写了一个程序,我想在其中计算幂集大小。但是我无法执行此操作,因为语句 1 的输出是 0?为什么显示错误?

public class PowerSetDemo 
{
public static void main(String s[])
{

int q=0;
char a[]={'a','b'};
int p=a.length;
System.out.println(p);

q=2^p; //error here
System.out.println(q); Why 0?

}
}

显示输出20

最佳答案

使用Math.pow而不是^
^ 是按位运算,如前所述 here

引用Math.pow了解更多信息。

关于java - 为什么java程序使用^运算符时的输出是这样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com