Skip to content

Commit 5ce630a

Browse files
committed
while循环
1 parent 7d433cc commit 5ce630a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Part3/Retirement.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Retirement {
4+
public static void main(String[] args) {
5+
Scanner in = new Scanner(System.in);
6+
7+
System.out.print("How much money do you need to retire? ");
8+
double goal = in.nextDouble();
9+
10+
System.out.print("How much money will you contribute every year? ");
11+
double payment = in.nextDouble();
12+
13+
System.out.print("Interest rate in %: ");
14+
double interestRate = in.nextDouble();
15+
16+
double balance = 0;
17+
int years = 0;
18+
19+
while (balance < goal) {
20+
balance += payment;
21+
double intrest = balance * interestRate / 100;
22+
balance += intrest;
23+
years++;
24+
}
25+
26+
System.out.println("You can retire in " + years + " years.");
27+
}
28+
}
29+

0 commit comments

Comments
 (0)