CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   User Input help (http://www.codingforums.com/showthread.php?t=283098)

Bungie 11-27-2012 08:21 PM

User Input help
 
1 Attachment(s)
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.

Code:

/*
        Program:                Ahmed_Mohamed_Project2.java
        Author:                        A. Mohamed
        Date:                        Nov, 23/2012
        Description:        A program that simulates a self checkout machine
*/

import java.util.Scanner;

public class fff
{
        public static void main(String[] args)
        {
       
        double runningtot;
        Scanner input = new Scanner (System.in);
        double[]runningArray = new double[14];
       
       
       
        System.out.println("Big Box Depot Self-Serve Checkout");
        System.out.println("");
        System.out.println("Hi. Welcome to the checkout. What's your name? ");
        System.out.println("");
       
       
        String name = input.nextLine();
        System.out.println("");
        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("");
        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("");
        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.");
        System.out.println("");
       
        for(int count = 0; count < runningArray.length;  count++)
        {
       
        System.out.println("Please enter price of item: ");
        double price = input.nextDouble();
                System.out.println("");
                System.out.println("That was $" + price);
                System.out.println("");
                runningtot = price;
                                System.out.println("Your total price is $" + runningtot);
                               
        }
       
       
        }
}

IMAGE ATTACHED

niralsoni 11-28-2012 11:20 AM

First, initialise your variable to zero
Code:

double runningtot = 0.00;
Then, in the for loop, keep on adding the amount rather then updating it.
Code:

runningtot += price;
Hope this helps you out...

Regards,
Niral Soni

DanInMa 11-28-2012 01:13 PM

wrong forum sir, you want JAVA not JavaScript


All times are GMT +1. The time now is 01:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.