2012-12-17 95 views
3

我想在這個靜態方法返回一個對象:靜態方法什麼都不返回? (試圖返回一個toString)

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 

正如你所看到的,它確實打印合併成功的消息,表明它去通過正確的循環,它只是不返回帳戶摘要。

回答

5

你從來沒有真正做回任何有價值的東西:

Account.Consolidate(acct2, acct3); 
+0

對不起,你是什麼意思? – user1740066

+1

您沒有將Account.Consolidate(acct2,acct3)的返回值設置爲任何值。應該是類似 賬戶consolidatedAcc = Account.Consolidate(acct2,acct3); 然後用返回值做一些事情(即打印內容) – Bizmarck

+0

感謝您的解釋。 – user1740066

1

除了通過其他的答案中發現的問題:

  • 您沒有設置在創建新的賬戶餘額通過consolidate

  • 使用double作爲帳號有點奇怪。

  • 使用double作爲帳戶餘額不正確。一個賬戶應該始終持有一定數量的美分,或者便士,或者其他什麼。 (去檢查你的銀行對帳單或問一個會計學生。)這就要求你使用整數類型......因爲涉及浮點類型的算術很容易出現舍入誤差。

  • 方法名稱應該以小寫字母開頭;例如Consolidate - >consolidate

+0

我通常聽到BigDecimal是一種代表金錢的類型。 – sdasdadas