I just started to learn java, and the first program I have to create is called PizzaRun, here is description:
PizzaRun is a simple program that calculates the number of pizzas to buy based on the sum of the slices entered on the command line arguments after the first argument, the price per pizza.
The example below shows an execution of the program on the command line (the dollar sign is the terminal window's prompt, and the # starts a terminal 'shell comment' to the end of the line).
Example: An 8-slice pizza costs $9.75 and four people want 1, 3, 4 and 2 slices.
$ java PizzaRun 9.75 1 3 4 2
# produces this output:
Buy 2 pizzas for $19.5
There will be 6 extra slices.
What I have so far is this:
import java.util.Scanner;
import java.util.ArrayList;
class PizzaRun {
public static void main(java.lang.String[] args){
System.out.println("buy" + calcWholePies(9) + "pizzas");
}
public static int slicePerPie = 8;
public static int calcWholePies(int nSlices)
{
int pies = 0;
while (nSlices > 0){
if (nSlices > slicePerPie){
nSlices = nSlices - slicePerPie;
pies++;
}
else{
pies++;
}
}
return pies;
}
}
I'm entering 9 into my calc method just for testing, but it doesn't seem to work when I run it through the terminal since nothing happens. I think it's getting stuck in an infinite loop or something.
Any tips? Is there a better way to calculate it other than a loop?
Thank you.