printf语句为同一个类的一个实例而不是另一个实例工作

时间:2014-03-21 22:13:33

标签: java

这是我第一次发帖,所以希望一切顺利。我遇到以下程序的问题。

public class Project3 {
    public static String fName = "drum_members.txt";
    private static Scanner fin;
    private static PrintWriter fout;
    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        String membershipLength;
        Member m_1 = new Member();
        Member m_2 = new Member();
        Member m_3 = new Member();
        Member m_4 = new Member();
        try {
            fin = new Scanner(new File(fName));
        } catch (FileNotFoundException e) {
            System.err.println("Error opening the file " + fName);
            System.exit(1);
        }// end try           

        m_1.Member();
        m_1.calculateFreeItems();
        m_1.printMember();
        m_2.Member();
        m_2.calculateFreeItems();
        m_2.printMember();
        m_3.Member();
        m_3.calculateFreeItems();
        m_3.printMember();
        m_4.Member();
        m_4.calculateFreeItems();
        m_4.printMember();        

    }
    public static class Member{
        public int id;
        public String name;
        public String nickName;
        public int monthsMembership;
        public String favoriteItem;
        public int freeItems;


        public void Member()
        {

            name = fin.next();
            nickName = fin.next();
            monthsMembership = fin.nextInt();
            favoriteItem = fin.next();
            fin.nextLine();

        }
        private int calculateFreeItems()
        {
            freeItems = monthsMembership/12 +1;
            return (freeItems);
        }
        public void setFavoriteitem()
        {
            System.out.print("Enter new favorite item: ");
            favoriteItem = keyboard.next();

        }
        private String calculatemembershipLength()
        {
            if(monthsMembership < 12)
                return (monthsMembership + "months,");
            else
                return (monthsMembership/12 + " years, " + monthsMembership%12 + " months,");
        }
        public void printMember()
        {
            String months = this.calculatemembershipLength();
            System.out.printf("Member #1 - NAME: %22s, NICKNAME:%22s, MEMBER SINCE: %22s FAVORITE ITEM:%22s, FREE ITEMS PER MONTH: %d\n",
                    name, nickName, months, favoriteItem, freeItems);
        }
    }
}

经过调试后,我知道printf语句在第三次工作时没有工作,第四次工作没问题。非常感谢任何帮助,感谢您的时间。

0 个答案:

没有答案