-2

The purpose of the program is to collect how much a person makes every week, add it up and display how much they make in a month. The for loop works just fine. My question is how do i add all the values of payweek and display them in the print statement outside the for loop.

import java.util.Scanner;
public class Burger
{
    static Scanner console = new Scanner (System.in);
    public static void main(String args [])
    {
        double payweek;
        int hours;
        for( int w = 1; w < 5; w++;){
            System.out.print("How many hours did you work in week" +w +"?");hours = console.nextInt();
            payweek = 0.7*5.15*hours;
            System.out.println("Your take home pay is $" + payweek);
        }
        System.out.println("Your total pay for the month is ");
    }
}
jrbedard
  • 3,662
  • 5
  • 30
  • 34
A.Garg
  • 59
  • 1
  • 9

2 Answers2

1
import java.util.Scanner; 
public class Burger { 
        static Scanner console = new Scanner (System.in); 
        public static void main(String args []) {

             double payweek;
             int hours;
             double monthPay = 0;
             for( int w = 1; w < 5; w++;){
                     System.out.print("How many hours did you work in week" +w+"?");hours = console.nextInt();
                     payweek =  0.7*5.15*hours;
                     monthPay = monthPay + payweek;
                     System.out.println("Your take home pay is $" + payweek);
           }
           System.out.println("Your total pay for the month is "+monthPay);
       }
    }

You just have to take variable which sums all payweek and then print it outside the loop. I think it helps.

Yash Mehta
  • 213
  • 3
  • 16
0
import java.util.Scanner; 
public class Burger { 

static Scanner console = new Scanner (System.in); 
public static void main(String args []) {        
    double payweek;
    double total=0;
    int hours;
    for( int w = 1; w < 5; w++;){
        System.out.print("How many hours did you work in week" +w +"?");hours = console.nextInt();
        payweek = 0.7*5.15*hours;
        System.out.println("Your take home pay is $" + payweek);
        total+=payweek;
    }
    System.out.println("Your total pay for the month is "+total);
}


}