2015-04-02 81 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

相关问题