【问题标题】:Keep Getting Error Message "cannot find symbol - constructor Account(int, double)不断收到错误消息“找不到符号 - 构造函数 Account(int, double)
【发布时间】:2012-02-18 17:32:23
【问题描述】:

修改类 Account(附在 Blackboard 中)以提供 • 一个整数类即时变量 accountNum 作为类号 (不要忘记您需要更改构造函数)。 • 一种称为借方的方法,用于从账户中提取资金。确保这件事 借方金额不超过账户余额。如果是这样,则 余额应保持不变,该方法应打印一条消息 表示“借方金额超过账户余额”。 • 在getBalance() 方法中,添加一行打印出帐号和 账户余额,然后在测试程序中,您只需要输入, account1.getBalance();而不是 printf 语句。 AccountTest 文件已被修改以测试您的新 Accountclass,因此您 不需要改变它。

这是我的帐户类代码

public class Account
{
    private double balance;
    public int accountNum;
    public int Account;
    public Account(double initialBalance, int accountN)
    {
        if ( initialBalance > 0.0 )
            balance = initialBalance;
            accountNum=accountN;
    }

    public void credit( double amount )
    {
        balance = balance + amount;
    }

    public void debit(double amount)
    {
        if (amount>=balance)
            System.out.println("Debit amount exceeded account balance."); 
        else
            balance-=amount;
    }

    public String getBalance()
    {
        return balance+" "+accountNum;
    } 
}

这是 AccountTest 类的代码

import java.util.Scanner;

public class AccountTest
{
    // main method begins execution of Java application
    public static void main( String args[] ) 
    {
        Account account1 = new Account( 1, 50.00 ); // create Account object
        Account account2 = new Account( 2, -7.53 ); // create Account object

        // display initial balance of each object
        account1.getBalance();
        account2.getBalance();

        // create Scanner to obtain input from command window
        Scanner input = new Scanner( System.in );
        double depositAmount; // deposit amount read from user

        System.out.print( "Enter deposit amount for account2: " );
        depositAmount = input.nextDouble(); // obtain user input
        System.out.printf( "\nadding %.2f from account1 balance\n", depositAmount );
        account2.credit( depositAmount ); // add amount to account1

        // display balances
        account1.getBalance();
        account2.getBalance();

        // create Scanner to obtain input from command window
        input = new Scanner( System.in );
        double withdrawalAmount; // withdrawal amount read from user

        System.out.print( "Enter withdrawal amount for account1: " ); 
        withdrawalAmount = input.nextDouble(); // obtain user input
        System.out.printf( "\nsubtracting %.2f from account2 balance\n", withdrawalAmount );
        account1.debit( withdrawalAmount ); // subtract amount from account2

        // display balances
        account1.getBalance();
        account2.getBalance();
    } // end main
} // end class AccountTest

我无法弄清楚缺少什么,请帮忙,非常感谢。

【问题讨论】:

  • 根据提示,我假设这可能是家庭作业

标签: java


【解决方案1】:

您只需让您的 Account 构造函数按该顺序接受一个 double 和一个 int。

所以在你的主要方法之后你有

Account account1 = new Account( 1, 50.00 );
Account account2 = new Account( 2, -7.53 );

应该是什么时候

Account account1 = new Account(50.00, 1);
Account account2 = new Account(-7.53, 2);

java 中的所有方法和构造函数只有在您传递的值与方法声明中的值完全一致时才能工作。一般每次看到“找不到符号”的错误都是这个问题。

【讨论】:

    【解决方案2】:

    account 类的构造函数需要一个 double 后跟一个 int,但您传递的是一个 int 后跟一个 double。

    【讨论】:

      【解决方案3】:

      帐户的构造函数被定义为接受doubleint,而这里你试图传递intdouble

      Account( 1, 50.00 );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        相关资源
        最近更新 更多