简体   繁体   中英

assigning array values and calculation are wrong

Q: Calculating the monthly units of electricity and getting the payment for every month.

  1. I am not getting jan, feb, march so on payment correctly. Monthly payments should be seperate. 110 = jan , 88 = feb and so on. If jan is less than 40, the value should multiply by 20.

  2. example: if jan = 35 then 35 *20 = to payment which is 700. So this should be applied for all months.

  3. int array jan, feb, march, april, may, jun, july, aug, sep, oct, nov, dec.

class test2 {

    public static void main(String[] args) {

        int months[] = {110, 88, 125,168,210,65,75,98,133,165,175,189};
        int x, size, count;
        size = months.length;
        double rate_chrg=0, payment =0;
        count = 0;
        x= 0;

        while (x<size){
            if (months[x] <40){
                payment = months[x] * 20;

            }
            else if(months[x] <60){
                payment = months[x] * 30;

            }
            else if(months[x] <80){
                payment = months[x] * 40;
            }
            else if(months[x] <250){
                payment = months[x] * 60;
            }
            x++;
        }

        System.out.println("Jan => " + months[0] + " = " +payment);
        System.out.println("Feb => " + months[1] + " = "+  payment);

    }
}

this is my output:

Jan => 110 = 11340.0
Feb => 88 = 11340.0

the amount amount is wrong, it should be printed like Jan => 110 = 6600 the above Answer 6000 is from 110 * 60 =6600

System.out.println("Jan => " + months[0] + " = " +payment);
System.out.println("Feb => " + months[1] + " = "+  payment);

put those line inside the while loop, and you will see the different, the problem is payment will be re-initial again after each loop in while

for January only this check

else if(months[x] <250)

is true, but this check is also true for March, April, May, September, October, November and December and because is iterating over the whole months array the payment is equal with the last element from the array which is only less the 250, which is December. So the payment it is 189*60

You need to either accumulate the payment or print the payment in the while loop

int months[] = {110, 88, 125,168,210,65,75,98,133,165,175,189}; String m[] = {"Jan", "Feb", "March", "April", "May", "June", "July", "Augest", "September", "October", "November", "December"}; int x, size, count, len; size = months.length; len = m.length; double payment =0;

x= 0;
count = 0;


  while (x<size && count<= len){
        if (months[x] <40){
          payment = months[x] * 20;

        }
        else if(months[x] <60){
          payment = months[x] * 30;
        }
        else if(months[x] <80){
          payment = months[x] * 40;
       }
       else if(months[x] <250){
         payment = months[x] * 60;
      }

      System.out.println(m[count] + ": "+ months[x] + " = " +payment);
      x++;
      count++;
}

// thank you and i did it... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM