我是一个新的Java学生,正在编写一个程序,它由一个main方法,一个类文件,两个input.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.
我不是在找施舍,这是我的第一个帖子在这里寻求帮助,但我知道你们不会为我解决这个...我只是需要学习如何做到这一点正确。我只是不知道为什么初始化有问题(所有提到的变量都是初始化的)和文件位于同一个文件夹。
我想您对初始化和声明之间的区别感到困惑。给出错误的变量确实声明了,但它们没有初始值。
看看w++
。这意味着:将1添加到w
。但是什么是w
?你没给出定义。
对于D
、AmountithDraw
和AmountDestory
也存在同样的问题
此外,使用文件作为输入的新扫描器需要FileNotFoundException
的try-catch语句。
最后,我想您的意思是filenameoutput=“output.txt”;
而不是filenameinputw=“output.txt”;
问题内容: 我是使用log4j软件包的新手,但看不到错误:这是一个非常简单明了的代码示例: 当我尝试编译时,出现此错误: my.package.logging.TestLogger.main(TestLogger.java:15)上的org.apache.logging.log4j.LogManager.getLogger(LogManager.java:129)处的线程“ main”中的java
我正试图在谷歌地图Android API v2中获得一个带有自定义图标的标记。我刚刚更改了Google提供的一个示例。我添加了到方法中的RawMapViewDemoActivity 但我总是得到一个“IBitmapDescriptorFactory未初始化”。 在BitmapDescriptorFactory中,它说: 在使用此类中的任何方法之前,您必须执行以下操作之一以确保初始化此类: > 等待
我最近安装了适用于Windows 64位的jdk-7u11。它最初运行得很好,我使用javac和java成功地编译和执行了我的java程序。但后来,当我使用javac编译一个文件时,它会顺利进行,当我继续使用java执行该文件时,会出现以下错误: VM初始化期间出错 java/lang/NoClassDefFoundError:java/lang/Object 我该怎么办?我是jdk的新手用户,我
问题内容: 我正在尝试使用File对象初始化FileInputStream对象。我在网上收到FileNotFound错误 这很奇怪,因为我已经通过相同的方法多次打开该文件来进行正则表达式。 我的方法如下: java.io.FileNotFoundException:C:\ dev的\服务器\的tomcat6 \的webapps \样品现场(访问被拒绝) 在java.io.FileInputStre
我有以下Java代码: 但是,当我运行它时,它会抛出以下错误: 我在do之前初始化了变量。while 循环,并在 try. 中设置值。捕获循环。似乎尚未设置该变量。抱歉,如果这是一个相当基本的问题,但我似乎无法弄清楚。
我用Xcode开发了一个“Hello iOS”应用程序,可以在iOS模拟器上用Appium进行测试。然而,当我试图在真正的iPhone/iPad设备上测试同一个应用程序时,我收到了以下错误消息。 这是我的环境。 我尝试了这个命令“brew安装--HEAD ideviceInster”,但得到了另一条错误消息。 有什么解决办法吗?谢谢