首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >帐户wd/dep类

帐户wd/dep类
EN

Stack Overflow用户
提问于 2015-12-12 21:53:12
回答 1查看 47关注 0票数 0

你们能帮我解决我在编写这个程序时遇到的一个问题吗?我得到了一个错误--我不知道为什么会在这方面出现语法错误:

主要方法:

代码语言:javascript
复制
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;
        }
    }
  }
}

支持方法:

代码语言:javascript
复制
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输出:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-12 21:58:37

这里需要分号:

代码语言:javascript
复制
private final double RATE = 0.03;//Semicolon here

您需要在Account类的顶部导入以下内容

代码语言:javascript
复制
import java.text.NumberFormat;

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

代码语言:javascript
复制
char choice = scan.next().charAt(0);

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

代码语言:javascript
复制
acct[key].getBalance()
acct[key].addInterest()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34245428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档