Reputation: 29
Here is my assignment:
Write a program that will emulate a cash register. Prompt the user to input the price of three items. Add them together to get a subtotal. Determine the tax (6% ) on the subtotal. Find the total amount of the sale subtotal plus tax. Display the price of each item, subtotal amount, tax amount and final amount.
I have it done like this:
package cashregisteremulator;
import java.util.Scanner;
public class CashRegisterEmulator {
public static void main(String[] args) {
Scanner price = new Scanner(System.in);
System.out.print("Please enter a price for item number one $");
double price1 = price.nextDouble();
System.out.print("Please enter a price for item number two $" );
double price2 = price.nextDouble();
System.out.print("Please enter a price for item number three $");
double price3 = price.nextDouble();
double total = ((price1) + (price2) + (price3));
System.out.println("The subtotal is $" + total);
double tax = .06;
double totalnotax = (total * tax );
System.out.println("The tax for the subtotal is $" + totalnotax);
double totalplustax = (total + totalnotax);
System.out.println("The total for your bill with tax is $" + totalplustax);
}
}
However, I need to do this assignment using a loop. Since the prompt asks for only 3 iterations, I thought of using a for loop. So far I have this:
package CashRegister;
import java.util.Scanner;
public class CashRegister {
public static void main(String[] args) {
Scanner askPrice = new Scanner(System.in);
for(double i = 0 ; i < 3; i++);
{
System.out.println("Enter a value") ;
double price = askPrice.nextDouble();
}
}
}
What do I have to do in order to ask the user for a price three times?
Upvotes: 1
Views: 12441
Reputation: 22094
double sum = 0;
for(int i = ; i < n; i++)
{
//ask for value;
sum += value;
}
update
in your update this must go into the for loop:
double[] amount = new int[3];
for(int i = 0; i < amount.len; i++)
{
// ask user for value
amount[i] = value;
}
for(int i = 0; i < amount.len; i++)
{
// do your calculations and print it.
}
// print the total sums.
For the totals you will need extra variables.
Upvotes: 1
Reputation: 245489
I'm not going to write all of your code for you, but this can be easily achieved using a for loop:
int numberOfIterations = 3;
// The format of the for loop:
// for (initialize counter; continuation condition; incrementer)
for(int i = 0; i < numberOfIterations; i++){
// do something
}
Upvotes: 1
Reputation: 9795
You need to make three iterations and for such scenario, the best thing to use is a for loop. In order to store the user inputs, you need an array in which you store the values with the index of the current position in the for loop.
Take a look at this example I built on your code:
double[] price= new double[3];
for (int i=0; i<3; i++); {
System.out.println("Enter another ");
double price[i] = price.nextDouble();
}
Later on, you can iterator over the price array, and add all the values:
double total = 0.0;
for (int i=0; i<price.length; i++) {
total += price[i];
}
Upvotes: 0