显示 java 中 for 循环中存在的需要添加的值

Displaying values which needed to be added that are present in a for loop in java

该程序的目的是收集一个人每周赚多少钱,将其相加并显示他们一个月赚多少钱。 for 循环工作得很好。我的问题是如何添加 payweek 的所有值并将它们显示在 for 循环外的打印语句中。

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 ");
    }
}
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);
}


}
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);
       }
    }

你只需要获取对所有发薪周求和的变量,然后在循环外打印它。 我觉得有帮助。