English 中文(简体)
相互之间增加两倍[闭门]
原标题:Adding doubles to each other [closed]
  • 时间:2012-04-10 12:09:19
  •  标签:
  • java
  • double

I m having trouble writing a method to calculate the total of some orders for a university project. Eclipse says there s an error, detailing only that the + is an invalid AssignmentOperator.

一些细节:

  • There s no privacy issues.
  • quantity is an int.
  • getPrice() returns a double.
  • total is a double

这种做法可能的确很简单,但因为如此,寻找答案非常困难。


public double calculateTotal(){
    for(OrderItem currentItem:items){
        for(int i=0;i<currentItem.quantity;i++){
            total+currentItem.product.getPrice();
        }
    }
    return total;
}
最佳回答

我认为需要<代码>+=:

public double calculateTotal(){
    for(OrderItem currentItem:items){
        for(int i=0;i<currentItem.quantity;i++){
            total += currentItem.product.getPrice();
        }
    }
    return total;
}

在你的例子中,你只重复了两点,没有结果。 你们需要把结果分配给一个变量。 使用<代码>+=为 Total = Total + currentItem.product.GetPrice();

您还可能需要将<代码>完全变量初步化;但可能的话,该代码在你的班次中其他部分。

问题回答

你们不能只增加两条价值观,而不必这样做。 我怀疑你的话。

        total += currentItem.product.getPrice();

页: 1 使用<代码>+=:

total += currentItem.product.getPrice();




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

热门标签