Home > Software engineering >  Nested java loop not producing desired result
Nested java loop not producing desired result

Time:11-07

I am trying to write a program that calculates the annual raise on salary with different percentages over the course of 4 years. However, the outer loop does not increment the salary or reuse it to calculate the raise after 4 years as required. The program output is also not rounding off to two decimal places as required. What could I be doing wrong?

import java.util.Scanner;

public class AnnualRaise {
    public static void main(String[] args) {
        //create a scanner for user input
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter a salary: ");
        double salary = input.nextInt();
        double raise;
        int MAX_YEAR = 4;
        int MAX_RAISE = 5;
        
        if (salary != 0) {
            for (int raiseRate = 3; raiseRate <= MAX_RAISE; raiseRate  ) {
                System.out.println("Raise rate: "   raiseRate   "%");
                raise = Math.round((salary * (raiseRate * 0.01)) * 100 / 100);
                salary  = raise;
            
                for (int year = 1; year <= MAX_YEAR; year  ) {
                    System.out.println("         Year: "   year   ", Raise: $"   raise   ", Salary: $"   salary);
                }
            }   
        }
        else 
            System.out.println("Good bye!");    
    }
}

CodePudding user response:

So the mistake here is that you are not updating the values of salary & raise in the inner loop. so it prints the exact same values.

double n = salary;
if(salary != 0) {
    for(int raiseRate = 3; raiseRate <= MAX_RAISE; raiseRate  ) {
            salary = n;
            System.out.println("Raise rate: "   raiseRate   "%");
            
            
            for(int year = 1; year <= MAX_YEAR; year  ) {
                raise = Math.round((salary * (raiseRate * 0.01)) * 100 / 100);
                salary  = raise;
                System.out.println("         Year: "   year   ", Raise: $"   raise   ", Salary: $"   salary);
            
            }
        }   
}

So this part should work just fine.

CodePudding user response:

Here's my take with the addition of looping until the input is zero. I'm using BigDecimal instead of double as it provides more control over scale. However, it does add to the verbosity.

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

class AnnualRaise {
    static final int MAX_YEAR = 4;
    static final int MAX_RAISE = 5;

    public static void main(String[] args) {
        //create a scanner for user input
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a salary: ");
        BigDecimal salary = BigDecimal.valueOf(input.nextInt());

        while (salary.compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal raise;
            for (int raiseRate = 3; raiseRate <= MAX_RAISE; raiseRate  ) {
                BigDecimal raiseRateBD = BigDecimal.valueOf(raiseRate * .01).setScale(2, RoundingMode.UNNECESSARY);
                System.out.println("Raise rate: "   raiseRate   "%");

                for (int year = 1; year <= MAX_YEAR; year  ) {
                    raise = salary.multiply(raiseRateBD).setScale(2, RoundingMode.HALF_UP);
                    salary = salary.add(raise);
                    System.out.println(" After ... Year: "   year   ", Raise: $"   raise   ", Salary: $"   salary);
                }
            }
            System.out.print("Enter a salary: ");
            salary = BigDecimal.valueOf(Double.valueOf(input.nextInt()));
        }
        System.out.println("Good bye!");
    }
}
  • Related