+ 2
Lesson 19: Loan Calculator Assistance
Currently doing the loan calculator and I’m having a bit of trouble figuring out what loop statement I should use for this. Rundown of it is taking a number, subtracting 10% of it, 3 times. What I am struggling with is figuring out how to have the new amount applied to the variable. Im thinking something along the lines of int x = 10 (Base amount of loan) int y = x / 10 (Find and assign 10% of Loan) int z = x - y How would I assign the new value of Z to X? Or is there a much more efficient way to do this?
13 odpowiedzi
+ 7
int x = 20000; // input amount;
for( int I = 0; I<3; I++)
x = x - x/10; // x -= x/10;
System.out.print(x) ;
any loop will works, not much differences.
+ 3
I'm pretty sure you can just assign "x = z" as your last line of calculation and it should come out fine.
I don't know if it's more efficient but you can definitely do the large part of the calculation in 1 line.
Something like
int x = inputAmount × 10%
+ 3
I don't think it gets any better than Jayakrishna🇮🇳 's for loop.
(Just quietly, he's a bit of genius and has helped me a lot too)
+ 2
x = z;
or it's equal to
amount = amount - amount /10;
+ 2
You have to declare variable only once. You can use that anytimes..
int x ; ==> declaring..
x = 20; => assigning...
x = x + 30;
+ 2
Okay, I see the error I was making. I kept putting
int amount = amount - blah blah
I had to remove the int. I love you guys lol, thank you
+ 2
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int amount = scanner.nextInt();
//your code goes here
int months = 3;
for (int i = 0; i < months; i++)
amount = amount * 90 / 100;
System.out.println(amount);
}
}
+ 1
So I can declare a new value to the variable even after its already been declared? It wouldnt produce an error saying that x has already been declared?
+ 1
I passed the challenge! Thank you lol. One last question. What would you guys suggest I do to clean this up? I went the extensive route by just typing it 3 times, but what loop would work best in this scenario & how would it be formatted? This is what I wrote to pass:
int x = amount / 10;
int y = amount - x;
amount = y;
x = amount / 10;
y = amount - x;
amount = y;
x = amount / 10;
y = amount - x;
amount = y;
System.out.println(amount);
+ 1
I got I got finally I passed
0
Int val to double
0
Just reassign x = z
And should be enclosed in a for loop
- 1
Heather Linn if you read this thread carefully, the answer is there.
If you want to receive help with your code, start a new thread and post your code there.