【问题标题】:Cannot find symbol method - Where should I define my methods? [duplicate]找不到符号方法 - 我应该在哪里定义我的方法? [复制]
【发布时间】:2016-03-08 23:13:39
【问题描述】:

我正在用 Java 编写一个银行程序,但在正确调用我的几个方法时遇到了一些麻烦。当我尝试编译我的文件时,这些是我得到的错误:

MustangBanking.java:74: error: cannot find symbol
            displayAccounts();
            ^
  symbol:   method displayAccounts()
  location: class MustangBanking

MustangBanking.java:75: error: cannot find symbol
            deleteAccount();
            ^
  symbol:   method deleteAccount()
  location: class MustangBanking
2 errors

我的包中的两个相关文件(此处仅粘贴相关部分)是 MustangBanking.java:

public class MustangBanking {

public static void main(String[] args) {

    //Declare and initialize data fields
//irrelevant code
case 3:
    displayAccounts();
    deleteAccount();
    break;
//more irrelevant code

和 Account.java

public class Account {

//Declare and initialize data fields
//irrelevant code
    public void deleteAccount() {
        //method code
    }
    public void displayAccounts() {
        //method code
    }
//irrelevant code

我在其他地方读到我的问题是,这两个方法应该在 MustangBanking 类而不是 Account 类中定义,并且它们应该在 main 方法之外定义。但是当我这样做时,我会收到无法找到所有变量等的错误。我在这里忘记了什么?如果您需要更多代码/说明,请告诉我,我会发布。

谢谢!

编辑: MustangBanking 类

import java.util.*;
import java.io.*;

//MustangBanking class
public class MustangBanking {

public static void main(String[] args) {

    //Declare and initialize data fields
    int id = 1000;
    double depositAmount = 0.0;
    double withdrawAmount = 0.0;
    double checkAmount = 0.0;
    double balance = 0.0;
    double annualInterestRate = 0.0;
    boolean run = true;
    int option;
    Scanner in = new Scanner(System.in);
    Account account = new Account();

    //Create ArrayList of type Account to store all Account objects
    ArrayList<Account> accounts = new ArrayList<>();

    //Loop to run the program inside of
    while (run) {

        //Display the menu
        System.out.println("\nMUSTANG BANKING MENU");
        System.out.println("\n1 -- Create a new checking account");
        System.out.println("2 -- Create a new savings account");
        System.out.println("3 -- Delete an existing account");
        System.out.println("4 -- View a specific account");
        System.out.println("5 -- View all accounts");
        System.out.println("6 -- Write checks for a checking account");
        System.out.println("7 -- Deposit funds into an account");
        System.out.println("8 -- Withdraw funds from an account");
        System.out.println("9 -- Find account with the largest balance");
        System.out.println("10 -- Exit");
        System.out.println("\nEnter Option:");
        option = in.nextInt();

        //Switch statement to direct program based on option entered by user
        switch (option) {

            //create a new checking account
            case 1:
            CheckingAccount c1 = new CheckingAccount(id, balance, annualInterestRate); //Create a new CheckingAccount object
            id++; //increment id by 1
            accounts.add(c1); //add the new CheckingAccount to the Arraylist accounts
            break;

            //create a new savings account
            case 2:
            SavingsAccount s1 = new SavingsAccount(id, balance, annualInterestRate); //create a new SavingsAccount object
            id++; //increment id by 1
            accounts.add(s1); //add the new SavingsAccount to the ArrayList accounts
            break;

            //delete an existing account
            case 3:
            //c1.displayAccounts();
            //c1.deleteAccount();
            break;

- 帐号类

import java.util.*;
import java.io.*;

//Account class
public class Account {

//Declare and initialize data fields
protected int id = 1000;
protected double balance = 0.0;
protected double annualInterestRate = 0.0;
protected double monthlyInterestRate;
protected Date dateCreated;
protected double depositAmount = 0.0;
protected int pendingChecks = 0;

Scanner in = new Scanner(System.in);

//Create ArrayList of type Account to store all Account objects
ArrayList<Account> accounts = new ArrayList<>(); //do i need to create the arraylist in every class?

//Delete account
public void deleteAccount() {
    System.out.println("\nPlease enter the ID of the account you wish to delete:");
    id = in.nextInt(); //take user input of id to delete
    accounts.remove(id); //remove the account
}

//Display all accounts
public void displayAccounts() {
    System.out.println("\nAvailable accounts:\n");
    for (int i = 0; i < accounts.size(); i++) {
        System.out.println(accounts.get(i).getId()); //print the id instead of the index
    }
}

//Display one account
public void displayAccount() {
    //Prompt user for the account they want to view
    System.out.println("\nPlease enter the ID of the account you would like to view:");
    id = in.nextInt();

    //Print the account information
    for (int i = 0; i < accounts.size(); i++) {
        if (accounts.get(i).getId() == id) {
            //if savings account
            if (accounts.get(i) instanceof SavingsAccount) { 
                System.out.println("Account type: Savings");
            }
            //if checking account
            else if (accounts.get(i) instanceof CheckingAccount) {
                System.out.println("Account type: Checking");
            }
        }
    }
    System.out.println("Account: " + id); //Print ID
    System.out.println("Balance: " + balance); //Print balance
    System.out.println("Date created: " + dateCreated); //Print date created
    System.out.println("Annual interest rate: " + annualInterestRate + "%"); //Print annual interest rate   
    //if checking account, print number of pending checks
    for (int i = 0; i < accounts.size(); i++) {
        if (accounts.get(i).getId() == id) {
            if (accounts.get(i) instanceof CheckingAccount) {
                System.out.println("Number of pending checks: " + pendingChecks);
            }
        }
    } 
}

【问题讨论】:

    标签: java methods cannot-find-symbol


    【解决方案1】:

    您在MustangBanking 类中调用Account 类的方法,而不使用Account 类的对象。这是导致问题的原因。

    尝试更改以下主要方法代码:

    public static void main(String[] args) {
        Account account = new Account();
        //Declare and initialize data fields
        //irrelevant code
    
        case 3:
            account.displayAccounts();
            account.deleteAccount();
            break;
        //more irrelevant code
    

    帐户类别 将以下方法添加到 Account 类

    public void add(Account a){
        accounts.add(a)
    }
    

    从所有的 switch case 中调用这个方法:

        //Switch statement to direct program based on option entered by user
        switch (option) {
    
            //create a new checking account
            case 1:
            CheckingAccount c1 = new CheckingAccount(id, balance, annualInterestRate); //Create a new CheckingAccount object
            id++; //increment id by 1
            account.add(c1); //add the new CheckingAccount to the Arraylist accounts
            break;
    
            //create a new savings account
            case 2:
            SavingsAccount s1 = new SavingsAccount(id, balance, annualInterestRate); //create a new SavingsAccount object
            id++; //increment id by 1
            account.add(s1); //add the new SavingsAccount to the ArrayList accounts
            break;
    
            //delete an existing account
            case 3:
            //c1.displayAccounts();
            //c1.deleteAccount();
            break;
    

    删除方法

    //Delete account
    public void deleteAccount() {
         System.out.println("\nPlease enter the ID of the account you wish to    delete:");
         id = in.nextInt(); //take user input of id to delete
         Iterator<Account> iAccount = accounts.iterator();
         while(iAccount.hasNext()){
              Account account = iAccount.next();
              if(account.getId()==id){
                    accounts.remove(account);
              }
         }
     }
    

    【讨论】:

    • 假设您的意思是“Account.displayAccounts();”等等,然后给我一个错误,即“无法从静态上下文引用非静态方法 displayAccounts()”。想不起来解决这个问题。
    • 您需要提供对象名称account.displayAccounts();。你用的是直接类名
    • 在这种情况下,我只是得到另一个找不到符号错误,但这一次它将帐户视为变量并说它找不到。我应该在某处声明一个随机帐户变量吗?或者我应该在某处使用我的代码中的其他东西。
    • 你有没有在我的回答中提到这一行Account account = new Account();
    • 我之前在不同的情况下有类似的代码——CheckingAccount c1 = new CheckingAccount(id, balance, annualInterestRate);SavingsAccount s1 = new SavingsAccount(id, balance, annualInterestRate);(CheckingAccount 和 SavingsAccount 都扩展了 Account)。这些本身算数吗?如果是这样,我将如何使用该语法来解决问题?
    【解决方案2】:

    您在没有创建 Account 类的对象的情况下调用 displayAccounts()deleteAccount(),因此您收到了错误

    使用对象调用方法有两种方式。

    1. 如果方法在同一个类中
    2. 如果有继承(MustangBanking extends Account)

    如果您在 Account 类中没有任何其他方法,则可以直接扩展 Account

    另外,在 MustangBanking 类中创建一个 Account 对象,然后调用上述方法如下

    Account account =new Account ();
    account.displayAccounts();
    account.deleteAccount();
    

    【讨论】:

    • 我尝试将这些方法放入同一个类中,但是当我将它们放在主要方法之外的 MustangBanking 类中时,我会收到无法找到我的变量的错误(这是有道理的,因为变量的范围仅在 main 方法内)。
    • @njwoodard 请详细说明,因为我不明白您的评论
    • @njwoodard 您的主要方法是静态的,如果您刚刚将该方法从 MustangBanking 复制到帐户,那么我希望您不要将这些方法设为静态。所以你得到了错误。所以请让方法静态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2016-05-13
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多