我会发布下面的完整代码。在我上一篇文章后,我试图让它成为自动格式。我希望不要太难阅读。无论如何...的问题....语法错误,文件读取器问题,setter问题。复杂的问题
我有三个问题。
第一)线113
的BufferedReader withdrawalFile = Files.newBufferedReader(取款,Charset.defaultCharset());
有一个错误“令牌上的语法错误”;“,{在此令牌之后预期。”
我完全不理解这个错误。如果你看94行,你会看到我写了“存款”的相同代码行,这没有同样的问题。
第二)线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
}
“有一个错误要求我为userAccount创建一个局部变量。”不,没有。发布* actual *错误消息。如果您有编译错误,则不可能出现运行时错误。你的问题没有意义。 – EJP 2014-12-08 09:17:58
@EJP他声称IDE说有句法错误。 – 2014-12-08 09:28:11
“在我上一篇文章之后,我试图让它具有自动格式,我希望它不会太难阅读。”如果代码没有语法错误,自动成形器只能完成它的工作。你的不是。你不应该依赖这个特性,并且确保你的代码格式化得有些不错。 – Turing85 2014-12-08 09:32:14