I need to re-initialize a user input in order to use it for the next iteration in a for loop...assuming for loop is the best choice for this. The loop is supposed to run three times and print out the final value gotten from the three iterations. In each iteration, it takes 10 percent of the amount entered by the user and subtracts it from the original amount. It then takes this new value and uses it in place of the initial value in the next iteration. However, instead of taking this new value, it retains the initial value and uses it again. I'm clearly doing something wrong, but I don't know what it is.
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int amount = scanner.nextInt();
for (int x=1; x<=3; x++){
int mod = (10/100);
int sub = amount*mod;
amount = amount-sub; // *** line 10 ***
}
System.out.println(amount);
}
}
This is the attempt I made at the code. The line I labeled as line 10 is my attempt at re-initializing the value "amount".
You need mod and sub to be floats then cast the result to int on the amount
Scanner scanner = new Scanner(System.in);
int amount = scanner.nextInt();
float mod = 10F / 100F;
for (int x=1; x<=3; x++){
float sub = amount*mod;
amount = (int) (amount-sub);
}
System.out.println(amount);