gpt4 book ai didi

Java编译器告诉我我还没有初始化变量 "interest"或 "pmt"

转载 作者:行者123 更新时间:2023-12-01 09:26:37 25 4
gpt4 key购买 nike

我正在尝试编写一个摊销计算器,用户可以在其中输入余额值、小数形式的利率值以及每月付款值。有了这些信息,我想输出以美元计的利息金额、本金金额和新余额。这是我的代码:

import java.util.Scanner;
public class Amortization{
public static void main(String []args){
Scanner pmt, interest, balance = new Scanner(System.in);
System.out.println("What is your balance?");
double b = balance.nextDouble();
System.out.println("What is your interest rate in decimal?");
double i = interest.nextDouble();
System.out.println("What is your monthly payment?");
double p = pmt.nextDouble();
double pv = p-(b*i);
System.out.println("Your interest amount is " + (b*i));
System.out.println("Your principal amount is " + pv);
System.out.println("Your new balance is " + (b-pv));
}
}

最佳答案

您不应声明 3 个扫描仪来读取标准输入。声明一个并继续阅读它。像这样:

import java.util.Scanner;
public class Amortization{
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println("What is your balance?");
double b = input.nextDouble();
System.out.println("What is your interest rate in decimal?");
double i = input.nextDouble();
System.out.println("What is your monthly payment?");
double p = input.nextDouble();
double pv = p-(b*i);
System.out.println("Your interest amount is " + (b*i));
System.out.println("Your principal amount is " + pv);
System.out.println("Your new balance is " + (b-pv));
}
}

这里的要点是扫描器是从输入流中读取的对象,而不是正在读取的值。您不需要一个新的扫描仪来读取您想要读取的每个值。

关于Java编译器告诉我我还没有初始化变量 "interest"或 "pmt",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780607/

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