I essentially need help with a program method where it decreases a input value (initialValue) by a percentage (6% for example) until it becomes either equal to or less than the next input value (requiredValue). The program is supposed to count how many times it decreases initialValue until it reaches or goes below requiredValue. Here is my basic concept code as of right now:
public static int howLongUntilRequired(int initialValue, int requiredValue) {
double newLevel = 0;
int count = 0;
while(initialValue != requiredValue && initialValue > requiredValue) {
newLevel = initialValue - (initialValue * 0.06);
count++;
}
return count;
}
I’m essentially stuck and I really don’t know where and how to continue/fix the code. It essentially outputs "0" for any user input. For example, a user input of (100, 40) should output "15". I have no clue if I’m overlooking a pretty basic function or I’m just not thinking hard enough LMAO. Any help or tips is appreciated.
>Solution :
"… The program is supposed to count how many times it decreases initialValue until it reaches or goes below requiredValue. …"
What you have is correct, just reduce the value of initialValue on each iteration.
Here is an example.
I copied the arguments to double variables x and y, and i will count the iterations.
public static int howLongUntilRequired(int initialValue, int requiredValue) {
double x = initialValue, y = requiredValue;
int i = 0;
while ((x -= (x * .06)) > y) i++;
return i;
}
"… For example, a user input of (100, 40) should output "15". …"
I believe it should be 14, iteration 15 would yield 39.
Here is the output for i and x.
0 94.0
1 88.36
2 83.0584
3 78.07489600000001
4 73.39040224000001
5 68.98697810560002
6 64.84775941926401
7 60.956893854108166
8 57.299480222861675
9 53.86151140948998
10 50.62982072492058
11 47.592031481425344
12 44.736509592539825
13 42.05231901698743
42.05 × 0.06 = 2.5, and 42 − 2.5 = 39.5