In the loop at each step, the iteration should become smaller, but for some reason after the second line the value remains unchanged.

import java.util.Scanner; public class HipotecalCalculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); double PERCENTS = 2.2; // System.out.println("сумма кредита "); double credit = 5000;//input.nextInt(); // System.out.println("первый взнос "); double firstPayment = 1000;// input.nextInt(); // System.out.println("Срок "); double paymentTerm = 12;//input.nextInt(); double leftToPay = credit - firstPayment; double Z = PERCENTS / 100 / paymentTerm; double linearProcent = leftToPay * Z; System.out.format("Линейный процент %.2f\n", linearProcent); double payPerMonth = linearProcent + (leftToPay / paymentTerm); for (double i = leftToPay; i > 0; i -= payPerMonth) { payPerMonth= linearProcent + (leftToPay / paymentTerm);//340.67 System.out.format("Проц %-6.2fМес %-8.2fОст %-8.2f\n", linearProcent, payPerMonth, i); linearProcent -= linearProcent - (leftToPay - payPerMonth) * Z; } } } 

This is what the code displays.

enter image description here

  • one
    You have some kind of nonsense here: linearProcent - = linearProcent - (leftToPay - payPerMonth) * Z; - Kirill Malyshev
  • As I see it .. 7.33- = 7.33- (4000-340.67) * 0.0018 7.33- = 7.33-6.71 7.33- = 0.62 from all this I expect that in the next cycle linerProcent will be equal to 6.71 and this line will look like this: 6.71 - = 6.71- (3659.33-340.04) * 0.0018 6.71- = 6.71-5.97 6.71- = 0.67 I understand correctly that in the case of 7.33- = 0.62, this is 7.33 in the next cycle should be 6.71? - Max

1 answer 1

Line

 linearProcent -= linearProcent - (leftToPay - payPerMonth) * Z; 

in most cases (except overflow, Nan, Infinity, null) is equivalent to:

 linearProcent = linearProcent - (linearProcent - (leftToPay - payPerMonth) * Z); 

open brackets, we shorten:

 linearProcent = (leftToPay - payPerMonth) * Z; 

Neither leftToPay nor payPerMonth in the loop. Accordingly, linearProcent in the first iteration takes a value that does not change later.

I did not understand the idea, but the name leftToPay implies that the value should change in months, and it is always 4000 in code.

  • Tell me what to do to make payPerMonth change? And ... how to move to another line in the comments? - Max
  • To make it change, it needs to assign a new value. What exactly do you know better. I just understood why the value does not change. In the comments, the transfer of a line in my opinion is impossible. If you want to supplement the question with information, edit it (there is a link to "edit" for this) - default locale
  • I do not understand why leftToPay does not change, because it displays the changed values, because it is in the picture. - Max
  • @Max in the picture in the third column displays the loop counter i as seen in the output line: System.out.format("Проц %-6.2fМес %-8.2fОст %-8.2f\n", linearProcent, payPerMonth, i); - default locale