-2
我在寫一個程序,它從文本文件中獲取貸款金額,利率和總額。它應該將利率更新爲總利息,並且對於總付款也是如此。然後它應該計算每月付款。運算符不能應用於java字符串
我一直得到的錯誤運算符不能應用於包括貸款計算的Java字符串。我猜這是因爲你不能在計算中使用字符串?也許我錯了。我很難過。
例輸入:
56750.00 .065 72.00
43675.00 .075 48.00
64950.00 .045 36.00
24799.00 .085 48.00
我的代碼
import java.io.*;
import java.util.*;
import java.text.*;
public class DB4
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException
{
String loan;
int Count = 0;
double interest = 0;
double Numberofmonths = 0;
double totint;
double totpay;
double monthly;
Scanner inFile= new Scanner(new FileReader("Project4InData.txt"));
PrintWriter outFile = new PrintWriter("Project4.out");
while (inFile.hasNext())
{
loan = inFile.next();
interest = inFile.nextDouble();
Numberofmonths = inFile.nextDouble();
// calcs
totint = interest * loan;
totpay = totint + loan;
monthly = loan/12;
outFile.print("Loan Amount: " + loan);
outFile.print(" ");
outFile.println("Interest: " + totint);
outFile.print(" ");
outFile.println("Total paid: " + totpay);
outFile.print(" ");
outFile.println("Monthly payment: " + monthly);
}
inFile.close();
outFile.close();
}
}
那你爲什麼不把'loan'改成另一個變量類型,例如雙? –
文件中的值看起來像是一個'double',爲什麼要把'loan'變成'String'? –