English 中文(简体)
无法使用BigDecimal找到符号
原标题:Cannot find symbol using 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)x(365-2)x(365-3)等,取决于传入的nbr人的价值。

我想了解为什么我会收到以下错误消息:

找不到符号

方法substrat(int)

不是想讨论阶乘,而是想讨论BigDecimal(或BigInteger)的使用。我之所以使用BigDecimal,是因为在稍后的阶段我需要进行除法,从而得到浮点值。

编辑

编辑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.de减法(int)方法,只有BigDecimal-de减法(BigDecimal)

问题回答

因为该方法的名称是减法,而不是减法。

参数也必须是BigInteger:

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

您正试图从BigDecimal中减去int。由于BigDecimal类上没有减法(int x)方法,因此会出现找不到符号编译器错误。

打字——你拼错了“减法”。

应该是减去(用一个s)

每当您看到找不到符号消息时,您就试图使用不存在的方法或不存在的变量。大多数情况下(如本例)是由于拼写错误或没有导入类。

BigDecimal只能减去另一个BigDecimal。你在减去一个整数。看

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

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...