Iam doing a school assignment in Java, and I need some help to do some calculations in a method. Iam used to PHP, and I've appended a lot of my knowledge in the code, but this one I just cant figure out (I know how do do it without the function, but that requires much more lines of code which is stupid).
Here is some of my code:
public static void main (String[] args) { // User inputs calculate("Number of beers", 20, 1.50); } public static void calculate(String articleName, double numberOfX, double pricePerUnit) { double subTotal = numberOfX * pricePerUnit; System.out.printf("%-20s %-1s %10.2f\n", articleName, ":", subTotal); } This prints out a nice bill of the things I've bought. Furthermore I would like this method to add the totalprice to a (global?) variable which eventually shows the final price of all items. In PHP i usually wrote a variable named totalDue += subTotal;
Is there any way to do this in java? I would be a shame to write an entire new function to do the math if I just could add the total price of each item into a variable.