Results 1 to 5 of 5
  1. #1
    meaganicole is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default java program won't add up for me?

    Hi,

    I don't know what I've done wrong but my code won't calculate the numbers that I input:

    Java Code:
    import java.util.Scanner;
    /* Design, code and test a program to enable a user to type in four savers' balances and the percentage bonus (which is the same for everyone). The program should then output the new balances for each saver. */
    
    class saverBonus
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		double newBalance, bonus, fourSavers, sum, percentageBonus;
    		
    		sum = 0;
    		
    		System.out.print("Please input the first savers' balance: ");
    		fourSavers = input.nextDouble();
    		
    		System.out.print("Please input the second savers' balance: ");
    		fourSavers = input.nextDouble();
    		
    		System.out.print("Please input the third savers' balance: ");
    		fourSavers = input.nextDouble();
    		
    		System.out.print("Please input the fourth savers' balance: ");
    		fourSavers = input.nextDouble();
    		
    		sum = fourSavers + fourSavers;
    		
    		System.out.print(fourSavers + sum);
    	}
    }
    What do I need to alter to make it add up the numbers to the exact total?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: java program won't add up for me?

    For one try responding to folks who helped you in your other thread here. That may motivate folks to help you again.

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: java program won't add up for me?

    You keep input to the same variable, each input you override the value the was in it before.
    You want to input 4 numbers then you have 2 ways.
    1: you will have 4 diffrent places that you store the data and then sum them.
    2: you will have 1 place that you store that data and after each input you will sum it.

    1: you can can use an array of doubles or jus 4 doubles.
    double a,b,c,d;
    input for a;
    input for b;
    input for c;
    input for d;
    sum = a + b + c + d;

    2: use a single variable.
    double a;
    input double a;
    sum += a;
    input double a;
    sum += a;
    and so on..

    In case you need the 4 values, use the first option else use the second one :)

  4. #4
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: java program won't add up for me?

    First up all understand the fact that "Whenever you are declaring a class level variable, a particular memory location is assigned to that variable." Keep this in mind.


    Yeah..Currently you are using a single variable "fourSavers " to store all the user input values. SO when you input multiple values, data will get overriden each time. Only one value/date that you entered at last time will be stored in the variable and hence you cant do the operation. If you want to perform the operation, then declare four different variables and store each value in each variable and then try..

    Hope you understand:)

    try your luck.

  5. #5
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: java program won't add up for me?

    //Please try the following code brother.


    import java.util.Scanner;
    /* Design, code and test a program to enable a user to type in four savers' balances and the percentage bonus (which is the same for everyone). The program should then output the new balances for each saver. */

    class saverBonus
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);

    //Declare four different variables to store 4 different values say firstBalance,secondBalance,thirdBalance and fourthBalance
    double newBalance,firstBalance,secondBalance,thirdBalance ,fourthBalance, sum, percentageBonus;

    sum = 0;

    System.out.print("Please input the first savers' balance: ");
    firstBalance= input.nextDouble();

    System.out.print("Please input the second savers' balance: ");
    secondBalance= input.nextDouble();

    System.out.print("Please input the third savers' balance: ");
    thirdBalance= input.nextDouble();

    System.out.print("Please input the fourth savers' balance: ");
    fourthBalance= input.nextDouble();

    sum = firstBalance+ secondBalance+thirdBalance+fourthBalance;

    System.out.print(sum);
    }
    }

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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