我是一名新的Java学生,我正在编写一个程序,该程序包含一个main方法,一个类文件,两个输入.txt文件和一个output.txt文件。主要方法应询问用户帐户余额和年度利息是什么,并从各自的文件中导入存取款信息,然后在输出文件中显示类文件中的所有计算。 我最初写这个文件是为了让用户使用扫描仪询问所有这些输入,现在我正试图让它使用文件作为输入工作...这不是很好。
主要方法:
import java.util.Scanner;
import java.io.*;
public class SavingsAccountDemo {
public static void main(String[] args) {
//declare variables
double interest;
double startAmount;
double amountDeposit;
double amountWithdraw;
double d, w;
String filenameInputW, filenameInputD, filenameOutput;
PrintWriter oFile;
File wFile, dFile;
Scanner iFile;
Scanner key = new Scanner(System.in);
//get initial balance
System.out.println("Enter initial account balance: ");
startAmount = key.nextDouble();
//create SavingsAccount class object sending starting amount to constructor
SavingsAccount money = new SavingsAccount(startAmount);
//get annual interest rate
System.out.println("Enter annual interest rate: ");
interest = key.nextDouble();
//send interest rate to class
money.setInterest(interest);
//Retrieve withdrawals from withdrawal.txt
filenameInputW="withdrawal.txt";
wFile = new File (filenameInputW);
iFile = new Scanner (wFile);
while(iFile.hasNext())
{
double num;
num=iFile.nextDouble();
amountWithdraw += num;
if(amountWithdraw >= 0.1)
w++;
}
//send to SavingsAccount class
money.withdraw(amountWithdraw);
//Retrieve deposits from deposit.txt
filenameInputD="deposit.txt";
dFile = new File (filenameInputD);
iFile = new Scanner (dFile);
while(iFile.hasNext())
{
double num;
num=iFile.nextDouble();
amountDeposit += num;
if (amountDeposit >= 0.1)
d++;
}
//send to SavingsAccount class
money.deposit(amountDeposit);
//display retults
filenameInputW="output.txt";
oFile=new PrintWriter (filenameOutput);
oFile.println("The ending balance is: " + money.getBalance());
oFile.println("The total amount of withdrawls are: " + w);
oFile.println("The total amount of deposists are: " + d);
oFile.println("The annual interest rate is: " + money.getInterest());
}
}
我的班级文件
/** * @(#)SavingsAccount.java * * * @author * @version 1.00 2013/5/6 */ public class SavingsAccount { //variables private double interest; private double balance; //Constructor public SavingsAccount(double b) { balance = b; interest = 0.0; } //Accessors public void setInterest(double i) { interest = i; } public void setBalance(double b) { balance = b; } //Mutators public double getInterest() { return interest; } public double getBalance() { return balance; } //Withdraw method public void withdraw(double withdraw) { balance = balance - withdraw; } //Deposit method public void deposit(double deposit) { balance = balance + deposit; } //Adding monthly interest to the balance public void addInterest() { double x = ((interest/12) * balance); balance = balance + x; } }
我收到这些错误:
--------------------Configuration: -------------------- C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:43: error: variable amountWithdraw might not have been initialized amountWithdraw += num; ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:46: error: variable w might not have been initialized w++; ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:50: error: variable amountWithdraw might not have been initialized money.withdraw(amountWithdraw); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:62: error: variable amountDeposit might not have been initialized amountDeposit += num; ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:65: error: variable d might not have been initialized d++; ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:69: error: variable amountDeposit might not have been initialized money.deposit(amountDeposit); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73: error: variable filenameOutput might not have been initialized oFile=new PrintWriter (filenameOutput); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:75: error: variable w might not have been initialized oFile.println("The total amount of withdrawls are: " + w); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:76: error: variable d might not have been initialized oFile.println("The total amount of deposists are: " + d); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:37: error: unreported exception FileNotFoundException; must be caught or declared to be thrown iFile = new Scanner (wFile); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:55: error: unreported exception FileNotFoundException; must be caught or declared to be thrown iFile = new Scanner (dFile); ^ C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73: error: unreported exception FileNotFoundException; must be caught or declared to be thrown oFile=new PrintWriter (filenameOutput); ^ 12 errors Process completed.
我不是在寻找讲义,这是我在这里的第一篇求助文章,但我知道你们不会为我解决这个问题......我只需要学习如何正确地做到这一点。我只是不知道为什么初始化有问题(所有提到的变量都被初始化)并且文件位于同一个文件夹中。
答案 0 :(得分:0)
从您的代码中,您可以在最顶层写下以下内容:double amountWithdraw;
就是这样。这不是初始化,而是声明。
如果你的女朋友开始谈论孩子,这同样适用,对吗?如果她宣称“我想要孩子”,那就像她“初始化”(生下)一个孩子一样。两者可能同样有压力,但方式不同。
您需要键入double amountWithdraw = 0.0;
或在修改值之前添加以下内容:amountWithdraw = 0.0;
。这将解决你的一小部分问题。
答案 1 :(得分:0)
编辑:对于错误......
在声明它们时,您应该给出这些变量值。即int w = 0
等。
肮脏的快速修复:至于其他与异常相关的错误,为简单起见,您可以放置public static void main(String[] args) throws FileNotFoundException
以便在过渡期间使用。
正确修复:通常情况下, 应该在catch块中处理FileNotFoundException
,因为在willy-nilly重新抛出异常不是好习惯
答案 2 :(得分:0)
--------------------配置:-------------------- C:\ Users \ Home \ Documents \ JCreator Pro \ MyProjects \ SavingsAccount \ src \ SavingsAccountDemo.java:43:错误:变量amountWithdraw可能尚未初始化 amountWithdraw + = num;
好吧,你声明变量amountWithdraw为double,但是compilator不知道这个变量的值是什么,他不能添加num。所以声明后你需要初始化amountWithdraw。它可以是这样的:
amountWithdraw = 0;
这很简单。
答案 3 :(得分:0)
我认为您对初始化和声明之间的区别感到困惑。确实提供了产生错误的变量,但它们没有初始值。
看看w++
。这意味着:向w
添加1。但是什么是w
?你没有定义它。
同样的问题适用于d
,amountWithdraw
和amountDeposit
此外,具有文件作为输入的新扫描程序需要FileNotFoundException
的try-catch语句。
最后,我认为您的意思是filenameOutput="output.txt";
而不是filenameInputW="output.txt";