静态方法什么都没有? (试图返回一个tostring)

时间:2012-12-17 01:02:32

标签: java

我试图在这个静态方法中返回一个对象:

public static Account Consolidate(Account acct1, Account acct2){
        String name1 = acct1.getName();
        String name2 = acct2.getName();
        double acctNum1 = acct1.getAcctNumber();
        double acctNum2 = acct2.getAcctNumber();
        double balance1 = acct1.getBalance();
        double balance2 = acct2.getBalance();
        double balance3;
        Account acct3 = new Account(name1);

        if ((name1.equalsIgnoreCase(name2)) &&  (acctNum1 != acctNum2)){
            balance3 = balance1 + balance2;
            acct1.close();
            acct2.close();              
            System.out.println("Consolidation successful!");        
            return (acct3);                     
        }
        else 
             System.out.println("These accounts cannot be consolidated.");          
             return null;
        }

但它什么也没有返回,我不知道如何解决它。如,

这是驱动程序:

    import java.util.Scanner;

public class Consolidates{
    public static void main(String[] args){
    String name1;
    String name2;
    String name3;

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter the name of the first account holder");
    name1 = scan.next();
    System.out.println("Enter the name of the second account holder");
    name2 = scan.next();
    System.out.println("Enter the name of the third account holder");
    name3 = scan.next();
    System.out.println();
    Account acct1 = new Account(100, name1, 333333);
    Account acct2 = new Account(100, name2, 555555);
    Account acct3 = new Account(100, name3, 777777);
    System.out.println(acct1);
    System.out.println(acct2);
    System.out.println(acct3);
    System.out.println();
    System.out.println("Closing account 1...");
    System.out.println();
    acct1.close();  
    System.out.println();
    System.out.println("Attempting to consolidate accounts 2 and 3...");
    Account.Consolidate(acct2, acct3);
    System.out.println();
    System.out.println("Printing original 3 accounts");
    System.out.println(acct1);
    System.out.println(acct2);
    System.out.println(acct3);
    }
}

最后,这是输出:

Enter the name of the first account holder
rick
Enter the name of the second account holder
james
Enter the name of the third account holder
james

Name: rick
Account Number: 333333
Balance: 100.0
Name: james
Account Number: 555555
Balance: 100.0
Name: james
Account Number: 777777
Balance: 100.0

Closing account 1...


Attempting to consolidate accounts 2 and 3...
Consolidation successful!

Printing original 3 accounts
Name: CLOSED
Account Number: 333333
Balance: 0.0
Name: CLOSED
Account Number: 555555
Balance: 0.0
Name: CLOSED
Account Number: 777777
Balance: 0.0

正如您所看到的,它 打印合并成功消息,表明它正确地通过循环,它只是没有返回帐户摘要。

2 个答案:

答案 0 :(得分:5)

你实际上从未对返回值做任何事情:

Account.Consolidate(acct2, acct3);

答案 1 :(得分:1)

除了其他答案所确定的问题外:

  • 您未在consolidate创建的新帐户中设置余额。

  • 使用double作为帐号有点奇怪。

  • 使用double帐户余额不正确。帐户应始终保持精确的分数,或便士,或其他什么。 (去检查您的银行对帐单......或者询问会计学生。)这需要您使用整数类型...因为涉及浮点类型的算法容易受到舍入错误的影响。

  • 方法名称应以小写字母开头;例如Consolidate - > consolidate