||11-27-2012 08:21 PM
User Input help
I'm making a self checkout machine where the user inputs prices and it comes out with a total. I'm supposed to show them this total after they enter a new value, so if they enter 20, then total price is 20 and if they enter another 20 its 40 and so on.
My problem is, my code only shows how much they entered and not the total.
Author: A. Mohamed
Date: Nov, 23/2012
Description: A program that simulates a self checkout machine
public class fff
public static void main(String args)
Scanner input = new Scanner (System.in);
doublerunningArray = new double;
System.out.println("Big Box Depot Self-Serve Checkout");
System.out.println("Hi. Welcome to the checkout. What's your name? ");
String name = input.nextLine();
System.out.println("Okay, " + name + ", enter the price of each purchase in dollars and cents, and then push the ENTER key. For example, if item costs $5.99 enter 5.99");
System.out.println("If you make a mistake when you enter a price enter a zero for the next entry and the last price you entered will be subtracted from your running total.");
System.out.println("When you've entered all of your prices, enter -1 to indicate that you've finished your entries. Iíll then calculate what your total owing is.");
for(int count = 0; count < runningArray.length; count++)
System.out.println("Please enter price of item: ");
double price = input.nextDouble();
System.out.println("That was $" + price);
runningtot = price;
System.out.println("Your total price is $" + runningtot);
||11-28-2012 11:20 AM
First, initialise your variable to zero
double runningtot = 0.00;
Then, in the for loop, keep on adding the amount rather then updating it.
runningtot += price;
Hope this helps you out...
||11-28-2012 01:13 PM
|All times are GMT +1. The time now is 01:04 PM.
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.