English 中文(简体)
添加2项大决定价值[复制]
原标题:adding 2 BigDecimal values [duplicate]
class Point {

  BigDecimal x;
  BigDecimal y;

  Point(double px, double py) {
    x = new BigDecimal(px);
    y = new BigDecimal(py);
  }

  void addFiveToCoordinate(String what) {
    if (what.equals("x")) {
      BigDecimal z = new BigDecimal(5);
      x.add(z);
    }
  }

  void show() {
    System.out.print("
x: " + getX() + "
y: " + getY());
  }

  public BigDecimal getX() {
    return x;
  }

  public BigDecimal getY() {
    return y;
  }

  public static void main(String[] args) {
    Point p = new Point(1.0, 1.0);
    p.addFiveToCoordinate("x");
    p.show();
  }
}

奥基,我要增加2个比埃拉姆价值。 I m 使用双倍的构造(因为我认为这是可能的——在文件中有选择)。 如果我使用这一工具,我可以这样做:

x: 1
y: 1

当我使用系统时,印本显示我的兹变数,即:

z: 5
最佳回答

BigDecimal是不可改变的。 每次行动都有一个新案例,其中载有行动的结果:

 BigDecimal sum = x.add(y);

如果你想要改变,那么你就必须这样做。

x = x.add(y);

阅读javadoc真正有助于了解某一类别及其方法的工作。

问题回答

或许这是你所希望的:

BigDecimal z = new BigDecimal(5).add(x);

<代码>BigDecimal的每一条操作都回收了一个新的<代码>BigDecimal,但并未改变目前的情况。





相关问题
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 ...

热门标签