为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我不能用 BigInteger.ONE 替换 BigInteger("1")?

为什么我不能用 BigInteger.ONE 替换 BigInteger("1")?

牛魔王的故事 2021-12-10 15:03:19
我正在编写斐波那契程序。public class ImperativeFibonacci implements Fibonacci {  public BigInteger fibonacci(int n) {    BigInteger nextFibonacci = new BigInteger("1"), currentFibonacci = new BigInteger("1");     for (int i = 2; i <= n; i++) {      nextFibonacci = currentFibonacci.add(nextFibonacci);      currentFibonacci = nextFibonacci.subtract(currentFibonacci);    }    return nextFibonacci;  }}我想用以下内容替换第 3 行:BigInteger nextFibonacci = new BigInteger.ONE, currentFibonacci = new BigInteger.ONE;但它不起作用。怎么了?
查看完整描述

3 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

因为BigInteger.ONE是一个常量并且已经有一个实例。删除new。喜欢

BigInteger nextFibonacci = BigInteger.ONE, currentFibonacci = BigInteger.ONE;


查看完整回答
反对 回复 2021-12-10
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

因为ONE是类的一个final static字段,BigNumber和new关键字配对没有任何意义,你不想分配 a BigNumber.ONE,你想直接使用它,只需删除关键字:


BigInteger nextFibonacci = BigInteger.ONE;

BigInteger currentFibonacci = new BigInteger.ONE; 

您要做的是尝试使用new变量声明,例如:


BigNumber NUMBER = new BigNumber("1");

BigNumber other = new NUMBER;

              //  ^^^ doesn't make any sense


查看完整回答
反对 回复 2021-12-10
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

BigInteger nextFibonacci = new BigInteger.ONE

将其更改为:

BigInteger nextFibonacci = BigInteger.ONE


查看完整回答
反对 回复 2021-12-10
  • 3 回答
  • 0 关注
  • 271 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号