否则,如果我只在此代码中出错

时间:2019-05-28 16:25:51

标签: java

else if (typeOfTransaction == 2);

得到错误“ else”而没有“ if”错误

这是我几年前提交的一个项目,再次发现它,并对这个错误感到好奇,为什么我会收到此错误

public class BankAccount
{
private static String accountIdentification;
private static String accountHolder;
private static Double balance;
private static Double annualInterstRate;

public static void main(String[] args) throws Exception
{ 
  Integer counter = 0;  
  Double temp = 0.0;
  Integer month = 0;
  Integer amount = 0;
  Integer typeOfTransaction = 0;


 //importing 97867564534231_transactions.txt and accounts.txt
File accounts = new File("accounts.txt");
File accountTransaction = new File("97867564534231_transactions.txt");

//does accounts exsist
    if (accounts.exists())
    {
        //does orders_validated.txt exsist
        if(accountTransaction.exists())
        {
    Scanner accountsCheck = new Scanner(accounts);
    accountsCheck.useDelimiter(",|" +          System.getProperty("line.separator"));
    Scanner accountTransactionCheck = new Scanner(accountTransaction);
    accountTransactionCheck.useDelimiter(",|" + System.getProperty("line.separator"));


    //does carton_info.txt have information
    if (accountsCheck.hasNext())
    {
       BankAccount(accountsCheck.next(),accountsCheck.next(), temp.parseDouble(accountsCheck.next()),temp.parseDouble(accountsCheck.next()));
       if (accountTransactionCheck.hasNext())
    {
       while (accountTransactionCheck.hasNext())
       {
           typeOfTransaction = Integer.parseInt(accountTransactionCheck.next());
           amount = Integer.parseInt(accountTransactionCheck.next());
           month = Integer.parseInt(accountTransactionCheck.next());

           if (typeOfTransaction == 1);
           {
               deposit(amount);
            }
            else if (typeOfTransaction == 2);
            {
                withdraw(amount);
            }

        }

        System.out.println("Account #          Holder                            Balance");
        System.out.println("-------------------------------------------------------------------");
        System.out.println(accountIdentification + "        " + accountHolder + "       " + balance);
    }
    else 
    {

        System.out.print("97867564534231_transactions.txt is empty");

    }
    }
}
else
{
    System.out.print("97867564534231_transactions.txt does not exist.");
}
}   
else
{

    System.out.print("accounts.txt does not exist.");

}
    }
    //used to construct a BankAccount with default values for account identification, account holder's full name, balance and annual interest rate.
    public BankAccount()
    {
        BankAccount.accountIdentification = "";
        BankAccount.accountHolder = "";
        BankAccount.balance = 0.0;
        BankAccount.annualInterstRate = 0.0;
    }
    //used to construct a BankAccount with an assigned account identification and account holder's full name, with the other fields set to their defaults.
    public BankAccount(String accountIdentification, String accountHolder)
    {
        BankAccount.balance = 0.0;
        BankAccount.annualInterstRate = 0.0;
    }
    //used to construct a BankAccount with assigned values for all fields.
    public BankAccount(String accountIdentification, String accountHolder, Double balance, Double annualInterstRate)
    {

        accountIdentification = accountIdentification;
        accountHolder = accountHolder;
        balance = balance;
        annualInterstRate = annualInterstRate;

    }
    // returns the account identification of the BankAccount.
    public String getAccountIdentification() 
    {
        return BankAccount.accountIdentification;
    }
    //modifies the BankAccount's account identification.
    public static void setAccountIdentification(String accountIdentification) 
    {

        BankAccount.accountIdentification = accountIdentification;

    }
    //returns the BankAccount holder's full name.
    public String getAccountHolder() 
    {
        return BankAccount.accountHolder;
    }
    // modifies the BankAccount holder's full name.
    public static void setAccountHolder(String accountHolder) 
    {

        BankAccount.accountHolder = accountHolder;

    }
    //returns the annual interest rate assigned to the BankAccount.
    public Double getAnnualInterestRate() 
    {
        return BankAccount.annualInterstRate;
    }
     //modifies the BankAccount's annual interest rate.
    public static void setAnnualInterestRate(Double annualInterstRate) 
    {

        BankAccount.annualInterstRate = annualInterstRate;

    }
    //returns the balance of the BankAccount
    public Double getBalance() 
    {
        return BankAccount.balance;
    }
    // adds an amount of money to the balance of the BankAccount. When the amount to deposit is a negative value, the BankAccount balance should not be changed.
    public static void deposit(double amount) 
    {
        if (amount > -1)
        {
        balance = balance + amount;
    }
    }
    //subtracts an amount of money from the balance of the BankAccount. When the amount to withdraw is a negative value, the BankAccount balance should not be changed.
    public static void withdraw(double amount) 
    {
        if (amount > -1)
        {
        balance = balance - amount;
    }
    }
    //returns the amount of monthly interest accumulated for the BankAccount based on the current balance. For BankAccount balances less than $1,000, no interest is accumulated.
    public Double getMonthlyInterest() 
    {
        if (balance > 1000)
        {

            balance = getMonthlyInterest * (annualInterstRate);

        }

        return getMonthlyInterest;
    }


}

1 个答案:

答案 0 :(得分:2)

摆脱您的“;”在您的“ if”和“ else if”末尾

       if (typeOfTransaction == 1);
       {
           deposit(amount);
        }
        else if (typeOfTransaction == 2);
        {
            withdraw(amount);
        }

应该是

       if (typeOfTransaction == 1)
       {
           deposit(amount);
        }
        else if (typeOfTransaction == 2)
        {
            withdraw(amount);
        }