Results 1 to 17 of 17
Thread: [SOLVED] Stuck
- 03-30-2009, 01:36 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
[SOLVED] Stuck
I'm a little stuck, after it displays the last employees information I need it to display the total wages the company is paying but I'm not sure how to do that! Can someone help
Java Code:import java.util.Scanner; public class FigureWages { public static void main(String[] args) { Scanner input=new Scanner(System.in); //Recieve the number of employees System.out.print("Enter number of employees "); double numEmp=input.nextDouble(); for(int i=0; i<numEmp; i++){ //Enter and recieve employee number System.out.print("Enter employee number "); int empNum=input.nextInt(); //Enter and recieve base pay rate per hour System.out.print("Enter employees base pay rate per hour "); double basePay=input.nextDouble(); //Enter and recieve Number of hours worked System.out.print("Enter numbers of hours worked "); int numHour=input.nextInt(); //Calculate total wages double totalWages=0; if(numHour<=40){ totalWages=numHour*basePay;} else if(numHour<=60){ totalWages=(40*basePay+(1.5*basePay*(numHour-40)));} else { totalWages=(40*basePay+20*(1.5*basePay)+(2*basePay*(numHour-60)));} //Display all information System.out.print("Employee number " + empNum + "\nNumber of hours worked "+ numHour +"\nTotal Wages " + totalWages+"\n");} System.out.print("This program was written by Justeena Leonard"); } }
- 03-30-2009, 01:41 AM #2
Define another variable to contain the company's total wages and at the very end just add to it the total wages to it with each loop:
Luck,Java Code:CompanyTotalWages = CompanyTotalWages + totalWages;
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-30-2009, 01:47 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
You are going to have to be more specific I am new to java.
- 03-30-2009, 01:59 AM #4
OK..... I'm assuming you know how to define a variable... something like:
Define it before the loop and at the end of your loop add the line of code from my previous post. After the loop just print the value of the companyTotalWages.Java Code:double CompanyTotalWages;
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-30-2009, 02:12 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
When i ran it in the program it doesn't add all the total wages from all the employees it just tells me how much they have to pay out for that one person
-
Then you're doing it wrong. You may wish to post your new code.
- 03-30-2009, 02:39 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
Java Code:import java.util.Scanner; public class FigureWages { public static void main(String[] args) { Scanner input=new Scanner(System.in); //Recieve the number of employees System.out.print("Enter number of employees "); double numEmp=input.nextDouble(); double companyTotalWages; for(int i=0; i<numEmp; i++){ //Enter and recieve employee number System.out.print("Enter employee number "); int empNum=input.nextInt(); //Enter and recieve base pay rate per hour System.out.print("Enter employees base pay rate per hour "); double basePay=input.nextDouble(); //Enter and recieve Number of hours worked System.out.print("Enter numbers of hours worked "); int numHour=input.nextInt(); //Calculate total wages double totalWages=0; if(numHour<=40){ totalWages=numHour*basePay;} else if(numHour<=60){ totalWages=(40*basePay+(1.5*basePay*(numHour-40)));} else { totalWages=(40*basePay+20*(1.5*basePay)+(2*basePay*(numHour-60)));} //Display all information System.out.print("Employee number " + empNum + "\nNumber of hours worked "+ numHour +"\nTotal Wages " + totalWages + "\n"); companyTotalWages = companyTotalWages + totalWages;} System.out.print("\nThis program was written by Justeena Leonard"); } }
-
1) You're not declaring your summation variable before the for loop as was recommended. A new variable will just be created each time the loop iterates, and that's not what you want to do.
2) You're not summing with this variable. To sum, you have to add to what's already held by the variable. In other words either this
or this:Java Code:sumVariable = sumVariable + somethingElse;
(they do the same thing)Java Code:sumVariable += somethingElse;
- 03-30-2009, 02:46 AM #9
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
How am I not intializing the variable
- 03-30-2009, 02:46 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
excuse me declaring it
-
Please re-read what I wrote. I didn't say that you're not initializing the variable, or that you're not declaring it. I'm saying where you're declaring it. It must be before the for loop.
- 03-30-2009, 02:52 AM #12
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
umm ok i don't get it don't worry about it
-
Please look at this example:
Java Code:public class Fubar4 { public static void main(String[] args) { int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int i = 0; i < intArray.length; i++) { int sum1 = 0; sum1 = intArray[i]; // I'm not adding here. } // sum1 doesn't even exist outside of this loop. int sum2 = 0; // declared before the next for loop for (int i = 0; i < intArray.length; i++) { sum2 = sum2 + intArray[i]; } // since sum2 was declared outside of the for loop // it exists outside of the for loop System.out.println("sum2 = " + sum2); } }
- 03-30-2009, 03:18 AM #14
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
Java Code:import java.util.Scanner; public class FigureWages { public static void main(String[] args) { Scanner input=new Scanner(System.in); //Recieve the number of employees System.out.print("Enter number of employees "); double numEmp=input.nextDouble(); double companyTotalWages; double totalWages; companyTotalWages=0; for(int i=0; i<numEmp; i++){ //Enter and recieve employee number System.out.print("Enter employee number "); int empNum=input.nextInt(); //Enter and recieve base pay rate per hour System.out.print("Enter employees base pay rate per hour "); double basePay=input.nextDouble(); //Enter and recieve Number of hours worked System.out.print("Enter numbers of hours worked "); int numHour=input.nextInt(); //Calculate total wages if(numHour<=40){ totalWages=numHour*basePay;} else if(numHour<=60){ totalWages=(40*basePay+(1.5*basePay*(numHour-40)));} else { totalWages=(40*basePay+20*(1.5*basePay)+(2*basePay*(numHour-60)));} companyTotalWages=+totalWages; //Display all information System.out.print("Employee number " + empNum + "\nNumber of hours worked "+ numHour +"\nTotal Wages " + totalWages + "\n"); } System.out.println("Companies total wages is " + companyTotalWages); System.out.print("\nThis program was written by Justeena Leonard"); } }
-
and it's still not adding up total wages, is it?
imagine that totalWages holds 20420.44, and then the for loop makes another loop for another worker. What happens to the 20420.44 held by totalWages when this line is processed?
it disappears. AGAIN, you have to add the new wages to the old wages. please re-read my example.Java Code:totalWages=(40*basePay+(1.5*basePay*(numHour-40)));
- 03-30-2009, 03:32 AM #16
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
OMG it worked thank you so much!!!
-
Similar Threads
-
Im on my last lab!!!! And im stuck...:(
By clanboru15 in forum New To JavaReplies: 5Last Post: 03-13-2009, 01:44 AM -
Stuck on code...plz help
By trump101 in forum New To JavaReplies: 1Last Post: 11-26-2008, 12:48 AM -
really stuck now..
By shongo in forum Advanced JavaReplies: 2Last Post: 11-09-2008, 02:56 AM -
Stuck in need of help!
By Zombie_Leg! in forum New To JavaReplies: 1Last Post: 09-23-2008, 02:22 AM -
I am completely stuck
By jpnym15 in forum New To JavaReplies: 2Last Post: 11-14-2007, 06:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks