Results 1 to 5 of 5
- 01-02-2012, 02:33 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
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:
What do I need to alter to make it add up the numbers to the exact total?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); } }
Thanks
-
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.
- 01-02-2012, 05:51 AM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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 :)
- 01-02-2012, 07:54 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
- 01-02-2012, 07:59 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 04:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 12:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks