gpt4 book ai didi

java - 如何循环直到得到正确答案?

转载 作者:行者123 更新时间:2023-12-01 17:00:38 25 4
gpt4 key购买 nike

我正在尝试编写一些可以重复执行的程序,直到达到正确的数量。我的程序基本上是一个贷款程序,可以帮助您计算出还清贷款和利息需要多长时间。到目前为止,我设法打印出第一个月的数据(虽然不完全正确......),但我需要不断重复,直到还清贷款金额。我希望这是有道理的。

import java.util.*;
import java.io.*;

public class Project4{
static Scanner console = new Scanner(System.in);

public static void main(String[] args) throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
Scanner user_input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);

PrintWriter outFile = new PrintWriter("Project4.out");

double lamnt;
double mpay;
double intrestrate;
double mnthintrest;
double mintrestrate;

System.out.print("Enter the Loan Amount: ");
lamnt = keyboard.nextDouble();
System.out.println();;

System.out.print("Enter the intrest rate (Ex:5.75): ");
intrestrate = keyboard.nextDouble();
System.out.println();

System.out.print("Enter the monthly payment you want to make: ");
mpay = keyboard.nextDouble();
System.out.println();

mintrestrate = intrestrate/12;
mnthintrest = lamnt*mintrestrate;

System.out.println("The first month intrest rate is:" + pay);
}
}

有人建议我使用 while 循环,但我不太确定如何使 while 循环继续运行,直到还清贷款金额。另外,是的,我知道结果不对,我正在处理那部分。我不太确定如何正确间隔标题。我需要的输出如下所示:(贷款付款使用 1000,利率使用 7.2,月付款使用 25)

付款#1后贷款金额为:981.00 本金为:19.00 利息为:6.00

付款#2后贷款金额为:961.89 本金为:19.11 利息为:5.89

最佳答案

因此,如果您想使用 while 循环直到还清贷款,只需执行以下操作:

while (lamnt > 0) {
// Do stuff to pay off the loan here.
}

只要您在循环内更新 lamnt,就应该可以工作。

希望有帮助!
;)

编辑:另外,请确保您只创建实际使用的扫描仪。并且不要忘记在范围结束时关闭它们!

关于java - 如何循环直到得到正确答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61509383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com