求一个计算银行本息问题的程序
现有1000元钱利率和年数自己从控制台输入利率每年增加%3最后的钱是多少?要求要用while循环写...
现有1000元钱 利率和年数自己从控制台输入 利率每年增加%3
最后的钱是多少?
要求要用while 循环写 展开
最后的钱是多少?
要求要用while 循环写 展开
1个回答
展开全部
import java.util.Scanner;
public class Xunhuan3 {
/**
* @param args
*/
private static double money = 1000;// 初始本金
private Scanner sc;
private String readString() {
sc = new Scanner(System.in);
return sc.next();
}
private int readInt() {
sc = new Scanner(System.in);
return sc.nextInt();
}
private double readDouble() {
sc = new Scanner(System.in);
return sc.nextDouble();
}
private void getMoney() {
try {
System.out.println("请输入年数(int 型)");
int year = this.readInt();
System.out.println("请输入利率(double 型)");
double lilv = this.readDouble();
int i = 0;
double newMoney = money;
while (true) {
if (i < year) {
newMoney = newMoney * (1 + lilv + 0.03 * i);
i++;
} else {
break;
}
// String s=t.readString();
System.out.println("第" + i + "年本息:" + newMoney);
}
} catch (Exception e) {
System.out.println("请输入正确的年数和利率");
}
}
public static void main(String[] args) {
Xunhuan3 t2 = new Xunhuan3();
t2.getMoney();
while(true){
System.out.println("按'y'继续");
String choice=t2.readString();
if(choice.equalsIgnoreCase("y")){
t2.getMoney();
}else{
break;
}
}
}
}
public class Xunhuan3 {
/**
* @param args
*/
private static double money = 1000;// 初始本金
private Scanner sc;
private String readString() {
sc = new Scanner(System.in);
return sc.next();
}
private int readInt() {
sc = new Scanner(System.in);
return sc.nextInt();
}
private double readDouble() {
sc = new Scanner(System.in);
return sc.nextDouble();
}
private void getMoney() {
try {
System.out.println("请输入年数(int 型)");
int year = this.readInt();
System.out.println("请输入利率(double 型)");
double lilv = this.readDouble();
int i = 0;
double newMoney = money;
while (true) {
if (i < year) {
newMoney = newMoney * (1 + lilv + 0.03 * i);
i++;
} else {
break;
}
// String s=t.readString();
System.out.println("第" + i + "年本息:" + newMoney);
}
} catch (Exception e) {
System.out.println("请输入正确的年数和利率");
}
}
public static void main(String[] args) {
Xunhuan3 t2 = new Xunhuan3();
t2.getMoney();
while(true){
System.out.println("按'y'继续");
String choice=t2.readString();
if(choice.equalsIgnoreCase("y")){
t2.getMoney();
}else{
break;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询