亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我不能用 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 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號