Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-27-2012, 08:21 PM   PM User | #1
Bungie
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Bungie is an unknown quantity at this point
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
Click image for larger version

Name:	variable.jpg
Views:	42
Size:	45.3 KB
ID:	11733  
Bungie is offline   Reply With Quote
Old 11-28-2012, 11:20 AM   PM User | #2
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 143
Thanks: 3
Thanked 38 Times in 38 Posts
niralsoni is an unknown quantity at this point
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
niralsoni is offline   Reply With Quote
Old 11-28-2012, 01:13 PM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
wrong forum sir, you want JAVA not JavaScript
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:09 AM.


Advertisement
Log in to turn off these ads.