Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: User Input help

  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    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
    Attached Thumbnails Attached Thumbnails User Input help-variable.jpg  

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    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

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    wrong forum sir, you want JAVA not JavaScript


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •