My java program won't add up the numbers?
Hi,
I don't know why my java program won't add up the numbers but here is the code:
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 can I do to alter the code to make it add up the numbers together?
Thanks
Re: My java program won't add up the numbers?
Every time you ask for a new input you are over-writing the last one. You will need separate variables for each input.
Re: My java program won't add up the numbers?
Umm.... What will this code do?
Code :
fourSavers+=fourSavers;
// Assuming that fourSavers is initialized by 0
Re: My java program won't add up the numbers?
Idk about your code but i'm guessing
fourSavers = fourSavers + input.nextDouble(); would work.. Not sure if this would work but deffiently take ^'s advice.
Re: My java program won't add up the numbers?
seems like your expecting to add FOUR numbers from FOUR inputs? right?, then you should have 4 variables to hold EACH of the FOUR inputs, then work aroundwith those 4 and you might get the sum of the FOUR
Re: My java program won't add up the numbers?
Quote:
Originally Posted by
chronoz13
seems like your expecting to add FOUR numbers from FOUR inputs? right?, then you should have 4 variables to hold EACH of the FOUR inputs, then work aroundwith those 4 and you might get the sum of the FOUR
Why not using an array instead of creating four different variables? Or use a single variable and add it up for all four inputs as OP seems to calculate the sum of all inputs. So, there is no need to use four different variables or any array.
Re: My java program won't add up the numbers?
You are using too many variables, if u don't need them anymore u can just sum them to fourSavers, and just print.
Also u are summing sum and fourSavers in system.out.print();
i've tried it to see what was the solution and here it is:
Code :
import java.util.Scanner;
public class saverBonus{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double fourSavers = 0.00;
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();
System.out.print(fourSavers);
}
}
This if u don't need the values anywhere else, u only use one variable and just add the input to it
Re: My java program won't add up the numbers?
Quote:
Originally Posted by
Nhedro
i've tried it to see what was the solution and here it is:
...
Nhedro, please read the forum rules and the following article:
http://www.javaprogrammingforums.com...n-feeding.html