帐户wd / dep类

时间:2015-12-12 21:53:12

标签: java

你能帮助我解决我在编译这个程序时遇到的问题吗?我收到一个错误我不确定为什么我会收到语法错误:

主要方法:

import java.util.Scanner;
public class Account2
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    Account[] acct = new Account[30];
    System.out.println("Enter your account number (1-30): ");
    int key = scan.nextInt() - 1;
    int reset = 0;
    while (reset == 0)
    {
      System.out.println("Enter W for withdrawl; D for deposit; X to escape");
      char choice = scan.nextChar();

      if  (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd' || choice == 'x' || choice == 'X')
      {
        if (choice == 'W' || choice == 'w')
        {
          System.out.println("Enter amount to withdraw: ");
          Double withdraw1 = scan.nextDouble();
          acct[key].withdraw(withdraw1);
          System.out.println("User # " + key++ + " funds after withdraw: " + acct[key].getBalance + "$");
          System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest + "$");
          reset++;
        }

        if (choice == 'D' || choice == 'd')
        {
          System.out.println("Enter amount to deposit: ");
          Double deposit1 = scan.nextDouble();
          acct[key].deposit(deposit1);
          System.out.println("User # " + key++ + " funds after deposit: " + acct[key].getBalance + "$");
          System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest + "$");
          reset++;
        }
        if (choice == 'x' || choice == 'X')
          System.out.println("Thank You for using this bank.");
          reset++;
      }
      else
        {
          System.out.println("Invalid entry, please try again");
          reset = 0;
        }
    }
  }
}

支持方法:

public class Account
{
  private final double RATE = 0.03 //Interest is 3%

  private long acctNumber;
  private double balance;
  private String name;

  //Defines owner, account number, and initial balance.
  public Account(String owner, long account, double initial)
  {
    name = owner;
    acctNumber = account;
    balance = initial;
  }

  //deposits a specified amount and returns new balance
  public double deposit(double amount)
  {
    balance = balance + amount;
    return balance;
  }

  //withdraws the specified amount from the account and applies the fee
  //                                                  + returns balance
  public double withdraw(double amount)
  {
    int fee = 1;
    balance = balance - amount - fee;
    return balance;
  }

  //Adds interest to the account
  public double addInterest()
  {
    balance += (balance * RATE);
    return balance;
  }
  public double getBalance()
  {
    return balance;
  }

  //returns a one line description of the account as a string
  public String toString()
  {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    return acctNumber + "/t" + name + "/t" + fmt.format(balance);
  }
}

编译时的CMD输出:

Account2.java:14: error: cannot find symbol
      char choice = scan.nextChar();
                        ^
  symbol:   method nextChar()
  location: variable scan of type Scanner
Account2.java:23: error: cannot find symbol
          System.out.println("User # " + key++ + " funds after withdraw: " + acct[key].getBalance + "$");
                                                                                      ^
  symbol:   variable getBalance
  location: class Account
Account2.java:24: error: cannot find symbol
          System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest + "$");
                                                                                      ^
  symbol:   variable addInterest
  location: class Account
Account2.java:33: error: cannot find symbol
          System.out.println("User # " + key++ + " funds after deposit: " + acct[key].getBalance + "$");
                                                                                     ^
  symbol:   variable getBalance
  location: class Account
Account2.java:34: error: cannot find symbol
          System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest + "$");
                                                                                      ^
  symbol:   variable addInterest
  location: class Account
5 errors

1 个答案:

答案 0 :(得分:0)

这里需要一个分号:

private final double RATE = 0.03;//Semicolon here

您需要在帐户类

的顶部导入以下内容
import java.text.NumberFormat;

nextChar()不是Scanner类中的有效方法。试试这个:

char choice = scan.next().charAt(0);

getBalance和getInterest是方法,所以你必须在调用它们时添加括号:

acct[key].getBalance()
acct[key].addInterest()