Java 数组中循环的控制结构 int[]num={1,2,3,4,5}; LabExercise2 pass=新的LabExercise2(); 对于(i=0;i=30) { 工资=26*小时; totalonemonth=工资*4; System.out.println(“一周工资总额:RM”+工资); System.out.println(“1个月的工资总额:RM”+totalonemonth+“\n”);

Java 数组中循环的控制结构 int[]num={1,2,3,4,5}; LabExercise2 pass=新的LabExercise2(); 对于(i=0;i=30) { 工资=26*小时; totalonemonth=工资*4; System.out.println(“一周工资总额:RM”+工资); System.out.println(“1个月的工资总额:RM”+totalonemonth+“\n”);,java,arrays,loops,for-loop,Java,Arrays,Loops,For Loop,您/任何人可以帮助我吗。我这里有一个代码,但问题是我需要将我的所有结果添加到控制结构(totalonemonth)中,全部5个,但都不起作用。有什么问题吗?您应该将您的total变量初始化为0,并每次添加到它 int [] num={1,2,3,4,5}; LabExercise2 pass = new LabExercise2 (); for(i=0; i<num.length; i++){ System.out.print("Please Enter Your Hours: "

您/任何人可以帮助我吗。我这里有一个代码,但问题是我需要将我的所有结果添加到控制结构(totalonemonth)中,全部5个,但都不起作用。有什么问题吗?

您应该将您的total变量初始化为0,并每次添加到它

int [] num={1,2,3,4,5};
LabExercise2 pass = new LabExercise2 ();
for(i=0; i<num.length; i++){
    System.out.print("Please Enter Your Hours: ");

    BufferedReader hr = new BufferedReader(new InputStreamReader(System.in));
    hour = hr.readLine();
    hours = Integer.parseInt(hour);

    if(hours >= 30)
        {
            wage = 26*hours;
            totalonemonth = wage*4;
            System.out.println("Total Wages for 1 week: RM"+wage);
            System.out.println("Total Wages for 1 month: RM"+totalonemonth+"\n");
inttotalonemonth=0;
对于(i=0;i=30)
{
工资=26*小时;
合计月份+=工资*4;
...
另外,循环迭代不需要数组。可以这样做:

    int totalonemonth = 0;
    for(i=0; i<num.length; i++){
        ...
        if(hours >= 30)
        {
             wage = 26*hours;
             totalonemonth += wage*4;
             ...
for(int i=0;i<5;i++){
...
}

@NinaScholz done ma'amhours可以是30.5,因此
totalonemonth
应该是
float
。小时被解析为整数:
integer.parseInt(小时)
for (int i = 0; i < 5; i++) {
    ...
}