2015-04-02 43 views
-1

我想弄清楚如何調用並正確輸出我的方法。但是,在輸出中,只顯示一個方法調用(最後一個)。我怎樣才能讓它輸出所有的方法調用,而不僅僅是最後一個。似乎所有以前的方法調用都被覆蓋,只有最後一個方法調用仍然存在。我剛剛開始使用Java。任何幫助,將不勝感激。只有一個方法調用顯示在輸出

這裏是派對驅動類,我做5個方法調用。只有最後一個顯示在printParty方法中。

import java.util.ArrayList; 

public class PartyDriver 
{ 

public static void main(String[] args) 

{ 


    Party party = new Party(3, "David Beckham"); 

    ArrayList<String> guest = new ArrayList<>(); 

    party.addGuest("Roberto Baggio"); 

    party.addGuest("Zinedine Zidane"); 

    party.addGuest("Roberto Baggio"); 

    party.addGuest("Johan Cruyff"); 

    party.addGuest("Diego Maradona"); 

    party.printParty(); 

} // end main 

}//End Party Driver 

這裏是黨類與所有的我的方法:

import java.util.ArrayList; 

public class Party 
{ 


    // instance variables that will hold your data 
    // private indicates they belong to this class exclusively 


    private int maxGuests; 
    private String host; 
    private String guest; 



    //Constructor 
    public Party(int maxGuests, String host) 
    { 

    this.maxGuests = maxGuests; 
    this.host = host; 
    } 



    //getter 
    // define type of data to be returned 
    public String getHost() 
    { 
    return host; 
    } 



    //setter 
    // setters have type void b/c they return nothing 
    public void setHost(String host) 
    { 
    this.host = host; 
    } 


//************************************* 
//Method to add to guest list 
    public void addGuest(String guest) 
    { 

    this.guest = guest; 
    } 


//************************************* 
//Method to print party 
public void printParty() 
    { 
    System.out.println("Guest list for " + 
     this.host + "'s party is: \n\n" + 
     this.guest + ".\n"); 

    } // end Print Party method 


}//end class Party 

回答

0

沒有方法,但PrintParty()打印任何東西,因爲你還沒有告訴任何方法,但PrintParty()打印任何東西。

將輸出打印到控制檯的一種方法是使用System.out.println()

我已經調整了您的每種方法來在屏幕上打印某些內容。我還爲您的Party類添加了一個guests實例變量,並使您的addGuest(String guest)方法適當修改了該實例變量。

import java.util.ArrayList; 

public class Party 
{ 
    // instance variables that will hold your data 
    // private indicates they belong to this class exclusively 
    private int maxGuests; 
    private String host; 
    private ArrayList<String> guests; 

    //Constructor 
    public Party(int maxGuests, String host) 
    { 
     System.out.println("Initializing party with maxGuests: '" + maxGuests + "' and host: '" + host + "'"); 
     this.guests = new ArrayList<String>(); 
     this.maxGuests = maxGuests; 
     this.host = host; 
    } 

    //getter 
    // define type of data to be returned 
    public String getHost() 
    { 
     System.out.println("Setting host to: " + host); 
     return host; 
    } 

    //setter 
    // setters have type void b/c they return nothing 
    public void setHost(String host) 
    { 
     System.out.println("Setting host to: " + host); 
     this.host = host; 
    } 

    //************************************* 
    //Method to add to guest list 
    public void addGuest(String guest) 
    { 
     if (guests.size() < maxGuests) 
     { 
      System.out.println("Adding guest: " + guest); 
      this.guests.add(guest); 
     } 
     else 
     { 
      System.out.println("Guest list full"); 
     } 
    } 

    //************************************* 
    //Method to print party 
    public void printParty() 
    { 
     System.out.println("Guest list for " + 
     this.host + "'s party is: \n\n" + 
     this.guests + ".\n"); 
    } // end Print Party method 

    public static void main(String[] args) 
    { 

     Party party = new Party(3, "David Beckham"); 
     party.addGuest("Roberto Baggio"); 
     party.addGuest("Zinedine Zidane"); 
     party.addGuest("Roberto Baggio"); 
     party.addGuest("Johan Cruyff"); 
     party.addGuest("Diego Maradona"); 
     party.printParty(); 

    } // end main 

}//end class Party 
+0

好的,我會嘗試你提出的這個建議。但是我所說的是我的printParty方法應該返回的不僅僅是一個名字。假設返回3個名字。它只是返回最後一個名字是「迭戈馬拉多納」。我調用了addGuest方法5次。但只有一個名字(「Diego Maradona」)似乎被添加到Guest方法中。我無法弄清楚爲什麼其他名稱不會被添加到來賓列表中,因爲它們也是printParty方法的一部分。 printParty方法應該打印來賓列表中的所有名稱,並且只打印一個名稱。 – Fryguy 2015-04-02 12:28:41

相關問題