2014-03-05 15 views
0

说明: 编写一个名为TagMaker的类打印出标记。 (a)设置名称(b)设置组织(c)打印标签与名称和组织(d)清除名称和组织(e)打印空白标签。然后编写一个TagTester类来测试TagMaker类。java标记程序(清除扫描程序和测试程序类)

所以我得到的代码来接受用户输入并打印出一个标签......但我做到了没有测试人员班(我害怕那些,而且它不工作,当我尝试使用一个)有什么建议吗?),我试着用代码来清除扫描器,以便打印出一个空白标签,但它一直在搞乱程序,所以我把它拿出来了。

这是我到目前为止有:

import java.util.Scanner; 

//进口java.util.Locale中; // import java.io. *;

public class TagMaker { 

public static void main (String[] args) 
{ 
Scanner scannerObject = new Scanner(System.in); 


System.out.print("This program will print out a name tag"); 
System.out.println("for each delegate."); 
System.out.println("Please enter first name:"); 
String first = scannerObject.next(); 
System.out.println("Please enter last name:"); 
String last = scannerObject.next(); 
System.out.println("Please enter organization or affilation:"); 
String org = scannerObject.next(); 


System.out.println("###### " + "Annual Conference" + " ######"); 
    System.out.println("### NAME: " + first + " " + last + " ###"); 
    System.out.println("################################"); 
    System.out.println("### ORGANIZATION:" + org + "###"); 
    System.out.println("###############################"); 


    String junk = scannerObject.next(); 
} 
} 

回答

0
public class TagMaker { 
private String tagName; 
private String organization; 

public void setTagName(String tagName){ 
    this.tagName = tagName; 
} 

public void setOrganization(String organization){ 
    this.organization = organization; 
} 

public void clearTagName(){ 
    this.tagName = ""; 
} 

public void clearOrganization(){ 
    this.organization = ""; 
} 

@Override 
public String toString() { 
    return "Tag [Name=" + tagName + "\n Organization=" 
      + organization + "]"; 
} 


} 

class TagTester{ 

    public static void main(String args[]){ 
    TagMaker customTag = new TagMaker();    //Creates a new tag 
    customTag.setTagName("Custom Name");    //Sets tag name to Custom Name 
    customTag.setOrganization("Custom Organization"); //Sets tag organization to Custom organization 
    customTag.clearTagName();       //Clears tag name 
    customTag.clearOrganization();      //Clears organization 
    System.out.println(customTag);      //Prints tag name and organization 
} 
}