我将在下面发布完整的代码。在我上一篇文章之后,我试图让它自动格式化。我希望阅读起来并不难。无论如何......问题......
我有三个问题。
1st)第113行
BufferedReader withdrawFile = Files.newBufferedReader(Withdrawals,Charset.defaultCharset());
出现错误“令牌上的语法错误”;“,{此标记后的预期”。
我根本不明白这个错误。如果你看第94行,你会看到我为“存款”编写了相同的代码行,但是没有相同的问题。
第2行)第119行
userAccount.setWithdrawal(userWithdrawal);
要求我为userAccount创建一个局部变量时出错。我认为这与上面的错误相关,但我试过寻找大括号或其他东西。我找不到任何东西。
第3次)我的二传手错误?
账户金额应该以610.17美元结束。它以500.00结尾文件阅读器应该编辑帐户中的金额上涨$ 110.17,但它不是,我不知道为什么。
此外,欢迎任何有关如何使我的代码更清洁的建议。
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.JOptionPane;
public class Savings {
private double accountAmount;
private double interestRate;
private int months;
//Provides access to interestRate, accountAmount, and months with getters
// and setters
public void setBeginning(double testAccount){
this.accountAmount= testAccount;}
public void setAccountAmount(double userInput){
this.accountAmount = userInput;
}
public void setWithdrawal(double withdrawal){
this.accountAmount = (accountAmount- withdrawal) ;}
public void setDeposit(double deposit){
this.accountAmount = (accountAmount + deposit);}
public void setMonths(int userMonths){
this.months = userMonths;}
public void setInterest(double userInterest){
this.interestRate= userInterest;}
public int getMonths(){
return this.months;}
public double getInterest(){
return this.interestRate;}
public double getAccount(){
return this.accountAmount; }
// Main method. Creates a savings account (userAccount) gets an interest rate and # of months
//from the user.
// Reads pre-written deposit and withdrawal files, account should end up at $610.17
public static void main (String [] args)throws IOException{
String stringInterest, stringMonths;
int userMonths;
double userInterest, preInterestAccount;
Savings userAccount = new Savings();
stringInterest = JOptionPane.showInputDialog("What is the monthly interest rate "
+ "on your account?\n Put it in decimal terms.\n (Ex: 1.0 would be 100%)");
if (stringInterest == null) {
System.exit(0); }
while (stringInterest.trim().length()== 0 || Double.parseDouble(stringInterest) < 0){
stringInterest= JOptionPane.showInputDialog("You did not enter a valid value.\n" +
"Please enter a valid value for interest.");
if (stringInterest == null){
System.exit(0);}
}//ends the while
stringMonths = JOptionPane.showInputDialog("How many months will the money be "
+ "in your account?");
if (stringMonths == null) {
System.exit(0); }
while (stringMonths.trim().length()== 0 || Integer.parseInt(stringMonths) < 0){
stringInterest= JOptionPane.showInputDialog("You did not enter a valid value.\n" +
"Please enter a valid value for interest.");
if (stringInterest == null){
System.exit(0);}
}//ends the while
userMonths=Integer.parseInt(stringMonths);
userInterest=Double.parseDouble(stringInterest);
userAccount.setMonths(userMonths);
userAccount.setInterest(userInterest);;
userAccount.setBeginning(500.00);
Path Deposists = Paths.get("C:\\Users\\Owner\\workspace\\BankAccount\\"
+ "src\\Deposists.txt");
BufferedReader depositFile = Files.newBufferedReader(Deposists, Charset.defaultCharset());
String dLine;
// read each line
while((dLine = depositFile.readLine()) != null) {
double userDeposit;
userDeposit=Double.parseDouble(dLine);
userAccount.setDeposit(userDeposit);
}
depositFile.close();}
Path Withdrawals = Paths.get("C:\\Users\\Owner\\workspace\\BankAccount\\"
+ "src\\Withdrawals.txt");
String wLine;
BufferedReader withdrawalFile = Files.newBufferedReader(Withdrawals, Charset.defaultCharset());
while((wLine = withdrawalFile.readLine()) != null) {
double userWithdrawal;
userWithdrawal=Double.parseDouble(wLine);
userAccount.setWithdrawal(userWithdrawal);
}
withdrawalFile.close();
preInterestAccount = userAccount.getAccount();
userAccount.setAccountAmount((userAccount.getAccount()*(1+userAccount.getInterest())*
(userAccount.getMonths())));
JOptionPane.showMessageDialog(null, "The interest rate is "+ (userAccount.getInterest()*100)
+"%.\n"+ "Your money was in the account for " + userAccount.getMonths()+" months.\n"
+ "The balance on your account before interest is "+ preInterestAccount + ".\n"
+ "The final balance of your account is "+ (userAccount.getAccount()) +".");
}//ends main method
}
答案 0 :(得分:0)
下面的while循环中有两个右括号。这导致第113行和第119行出错。只需在depositFile.close();
之后删除括号。
// read each line
while((dLine = depositFile.readLine()) != null) {
double userDeposit;
userDeposit=Double.parseDouble(dLine);
userAccount.setDeposit(userDeposit);
} // <--------------- HERE --------------------
depositFile.close();} // <------------ AND HERE ---
此外,你有一个不必要的分号,这不会导致错误,但它不应该在那里:
userAccount.setMonths(userMonths);
userAccount.setInterest(userInterest);; //<-------- HERE
userAccount.setBeginning(500.00);
为了使您的代码更清晰:如果您使用的是IDE(如Eclipse),那么应该有一个自动格式化代码的选项。在Eclipse中它是Source->format
。