2011-02-11 55 views
2

我正在做我第一次嘗試使用BigDecimal。這似乎很棘手,我遇到了一個問題,我想了解是什麼導致它。使用BigDecimal找不到符號

public static String nominator(String nbrPeople) 
{ 
    BigDecimal nom = new BigDecimal("365") ; 
    BigDecimal days = new BigDecimal("365") ; 
    int limit = Integer.parseInt(nbrPeople); 
    for (int i = 0 ; i < limit ; i++) 
    { 
     days = days.substract(i) ; 
     nom = nom.multiply(days) ; 
    } 
    return nbrPeople ; 
} 

這是一個更大的程序的一部分。它是應該計算是這樣的方法:

365 X(365-1)×(365-2)×(365-3)中傳遞等取決於nbrPeople的值

我將想明白,爲什麼我收到以下錯誤信息:

找不到符號

方法。減去(INT)

不找一個討論而不是使用BigDecimal(或BigInteger)。我正在使用BigDecimal,因爲在稍後階段,我需要分割,導致浮點。

EDIT

編輯2

第一編輯移除(代碼),以使後更readable-正確的代碼已經由一種程序員下面貼

+0

在回答前面已經提到。你不能減去我,因爲它不是BigInteger。 – RoflcoptrException

回答

2

這應該工作:

public static String nominator(String nbrPeople) 
{ 
    BigDecimal nom = new BigDecimal("365") ; 
    BigDecimal days = new BigDecimal("365") ; 
    int limit = Integer.parseInt(nbrPeople); 
    for (int i = 0 ; i < limit ; i++) 
    { 
     days = days.subtract(new BigDecimal(i)) ; 
     nom = nom.multiply(days) ; 
    } 
    return nbrPeople ; 
} 

因爲沒有BigDecimal.subtract(int)方法,只有BigDecimal.subtract(BigDecimal)方法。

+0

your're a Star :)謝謝! – raoulbia

+0

@ALL:感謝您的及時回覆。這一切現在更有意義! – raoulbia

+1

有趣的是,你犯了同樣的錯字 - 「減」的「減」。 –

2

錯字 - 你拼錯 「減」。

3

您正試圖從BigDecimal中減去int。由於BigDecimal類上沒有方法subtract(int x),因此會出現cannot find symbol編譯器錯誤。

2

subtract(具有單個或多個)

每當你看到找不到符號消息,您要使用一個不存在的方法或不存在的變量。大多數情況下(如本例)由於拼寫錯誤或因爲您沒有導入課程。

0

http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#subtract(java.math.BigDecimal

import java.math.BigDecimal; 
import java.util.Scanner; 

public class BigDecimal_SumExample { 

    public static void main(String args[]) { 

     BigDecimal number1; 
     BigDecimal number2; 
     BigDecimal sum; 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter the value of number 1"); 
     number1 = sc.nextBigDecimal(); 
     System.out.println("Enter the value of number 2"); 
     number2 = sc.nextBigDecimal(); 


     BigDecimal a = new BigDecimal(""+number1); 
     BigDecimal b = new BigDecimal(""+number2); 
     BigDecimal result = a.add(b); 

     System.out.println("Sum is Two numbers : -> "); 
     System.out.println(result); 

    } 
} 

**Output is** 

Enter the value of number 1 
68237161328632187132612387312687321678312612387.31276781237812 

Enter the value of number 2 
31232178631276123786321712369812369823162319862.32789129819299 

Sum is Two Big Decimal numbers : -> 
99469339959908310918934099682499691501474932249.64065911057111 
+0

http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#subtract(java.math.BigDecimal –