-2

我是 Java 新手,请帮助我,我的局部变量不能带我方法参数。

        import javax.swing.*;
        import java.util.*;
        import java.io.*;
        import java.text.DecimalFormat; //I can not get my local variables in my
                                             // main to accept my methods parameters.


       // This is my program.
         public class AccountBank
         {
        public static void main (String[] args) throws IOException
         {  
            // Calling in my Class
            Accountclass BankAcc = new Accountclass();

            // initialize both there variables in. order to use them in a for loop.
            double depDrw = 0;// this are one of the variables that is giving me problems
            double withDrw = 0; // this is the other that is giving me problems
            double totalW = 0;
            double totalD = 0;

            // declaring all my variables
            String name="";
            double month;
            double startBal;


// This section will greet and accept input by asking the user to enter the starting alance and set it in my class


            // Greetings
            JOptionPane.showMessageDialog(null,"Lets Get Started");

            // receiving input for my name variable
        name = JOptionPane.showInputDialog(null, "Please Enter Your Name Below: ");

            // ask user for starting balance
            startBal = Double.parseDouble(JOptionPane.showInputDialog("What Is The Starting Balance In Your Account:"));
            // This will set the value in my class  
            BankAcc.setBal(startBal); 

            // ask user how many months has the account been active
            month = Double.parseDouble(JOptionPane.showInputDialog("Months That Account Has Been Active:"));




            // This section will accept input by asking the user to enter each amount deposited every month from the account set it in my class.


                // This will be shown in the message box
                depDrw = depositTotal(deposit); << // I am having trouble here it wont take my parameters variable which I created on the buttom.  please help
                // This will sum up every amount the user enters in the message box.
                totalD += depDrw; 

                // This will set the value in my class
                BankAcc.setdeposit(totalD);

// 这部分将通过要求用户输入每个月从帐户中提取的金额并将其设置在我的类中来接受输入

                // This will be shown in the message box
                withDrw = withdrawTotal(wit); // <<< I am having problem here this variable does not take the value of my methods parameter, which i created on the bottom of this page.
                // This will sum up every amount the user enters in the message box.
                totalW += withDrw;
                // This will set the value in my class
                BankAcc.setwithdraws(totalW);




//This section will display the " monthly interest rate, monthly interest earned, total amount deposited, total amount withdrawn, and the final balance of the account." 

                DecimalFormat formatter = new DecimalFormat("#0.0000");
                DecimalFormat formatter2 = new DecimalFormat("#0.0");
                DecimalFormat formatter3 = new DecimalFormat("#0.00");

            //Get the calculations from the savings account class and display them.
                JOptionPane.showMessageDialog(null," Account Name:  " +name+"\n \n Your Monthly Interest Rate Is .....  " 
                + formatter.format(BankAcc.monthInt())+"%" + "\n \n Your Monthly Interest Earned Was .....  $"
                + formatter2.format(BankAcc.GetInt()) + "\n \n Your Overall Amount With Deposited Was .....  $" + totalD +
                " \n \n Your Overall Amount WithDrawn Was .....  $" + totalW  + " \n \n Your Remaining Balance Is .....  $"
                + formatter3.format(BankAcc.getFinalbal()),"Results", JOptionPane.PLAIN_MESSAGE );



    }

    public static double depositTotal( String deposit)
                                throws IOException
        {
            double sales;
            double totalDeposit = 0;

            File file = new File ("deposits.txt");
            Scanner inputfile = new Scanner(file);

            while (inputfile.hasNextDouble());
            {
                sales = inputfile.nextDouble();
                totalDeposit += sales;
            }

            inputfile.close();

            return totalDeposit;

        }

    public static double withdrawTotal( String wit)
                                throws IOException
        {
            double sales;
            double totalwithdraws = 0;

            File file = new File ("withdraws.txt");
            Scanner inputfile = new Scanner(file);

            while (inputfile.hasNextDouble());
            {
                sales = inputfile.nextDouble();
                totalwithdraws += sales;
            }

            inputfile.close();

            return totalwithdraws;

        }
4

2 回答 2

1

更改您的方法声明,使其不接收任何参数。而且因为它返回一个双精度值,你可以将它返回的值存储在一个double变量中:

double deposit = depositTotal();

在你的depositTotal()方法中:

public static double depositTotal() throws IOException {
... 
}
于 2013-11-01T19:15:56.777 回答
1

你的 while 循环是

while (inputfile.hasNextDouble());
        {
            sales = inputfile.nextDouble();
            totalDeposit += sales;
        }

不应该有一段;时间后(inputfile.hasNextDouble())

while (inputfile.hasNextDouble())
            {
                sales = inputfile.nextDouble();
                totalDeposit += sales;
            }

同样对于其他 while 循环,删除;

于 2013-11-01T19:15:59.747 回答