0

在我发布的这个程序中,当我调用(setter 函数)即。obj.setsize(res) 在主函数中比它无法将(在类“contact_entry”中声明的变量“size”)private int size=2 更改为 size=1。

当我调用(getter 函数)时,该值保持为 2,即。obj.getsize() 在主程序中,在 if(obj.getsize()>0). 当 set 方法被调用时,它应该设置变量“size to 1”的值,但它不能修改“大小保持不变”的值和值。

请让我知道我会很感激的问题。

    import javax.swing.JOptionPane;
    public class nokia {

        public static void main(String[] args) 
        {
            int itr=0;

            while(itr==JOptionPane.YES_OPTION)
            {
                String s3=JOptionPane.showInputDialog("Enter your option\nEnter 1 to add contact\nEnter 2 to delete contact\nEnter 3 to display contact" +
                    "\nEnter 4 to search a contact detail\nEnter 5 to exit");
                switch(s3)
                {
                case "1":
                {
                    contact_entry obj=new contact_entry();
                    if(obj.getsize()>0)
                    {
                        obj.add_contact();
                        int res=obj.getsize()-1;
                        obj.setsize(res);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "MEMORY FULL....NO MORE CONTACTS CAN BE ADDED");
                    break;
                }

                case "2":
                }

            }
        }

    }

    class contact_entry
    {
        private String fst_nme;
        private String lst_nme;
        private long [] mo_no=new long[3];
        private int[] hme_no=new int[3];
        private int size=2;

        public contact_entry()
        {
            fst_nme="ron";
            lst_nme="capton";
            mo_no[0]=mo_no[1]=mo_no[2]=0;
            hme_no[0]=hme_no[1]=hme_no[2]=0;

        }

        public void add_contact()
        {
            fst_nme=JOptionPane.showInputDialog("Enter the first name: ");
            lst_nme=JOptionPane.showInputDialog("Enter last name: ");

            int itr=0,itr1=0;
            for(int i=0;itr==JOptionPane.YES_OPTION;i++)
            {
                if (i>2)
                {
                    JOptionPane.showMessageDialog(null,"NO MORE MOBILE NUMBER CAN BE ADDED...MEMORY FULL");
                    break;
                }
                String s1=JOptionPane.showInputDialog("Enter mobile number: ");
                mo_no[i]=Long.parseLong(s1);
                itr=JOptionPane.showConfirmDialog(null, "Do you want to add addtional contact number");

            }

            for(int i=0;itr1==JOptionPane.YES_OPTION ;i++)
            {
                if(i>2)
                {
                    JOptionPane.showMessageDialog(null, "NO MORE PHONE NUMBER CAN BE ADDED....MEMORY FULL");
                    break;
                }
                String s2=JOptionPane.showInputDialog("Enter phone number: ");
                hme_no[i]=Integer.parseInt(s2);
                itr1=JOptionPane.showConfirmDialog(null, "DO you want to add more contact number");

            }   
        }

        public void display()
        {
            String output= "First Name: "+fst_nme+"\nLast name: "+lst_nme+"\nmobile number1: "+mo_no[0]+"\nmobile number2: "+mo_no[1]+
                    "\nmobile number3: "+mo_no[2]+"\nphone number1: "+hme_no[0]+"\nphone number2: "+hme_no[1]+"\nphone number3: "+hme_no[2];
            JOptionPane.showMessageDialog(null, output);
        }

        public int getsize()
        {
            JOptionPane.showMessageDialog(null, size);
            return size;
        }

        public void setsize(int size1)
        {
            this.size=size1;
            JOptionPane.showMessageDialog(null, size);
        }
       } 
4

4 回答 4

1

我无法用您的代码重现这一点。注释掉add_contact(因为它无关紧要)我看到三个对话框:

  • 2(第一次调用getsize()
  • 2(第二次调用getsize()
  • 1(打电话给setsize()

所以它将设置size为1。

请注意,当您循环并创建一个 contact_entry实例时,它的大小为 2……这让您感到困惑吗?

(顺便说一句,您应该真正尝试遵循 Java 命名约定。您的代码目前非常单一。)

于 2012-05-31T07:21:40.900 回答
1

您正在contact_entry每个循环中创建新对象。所以每次你有新的对象时size = 2。你正在向它添加新的联系人。因此,您没有看到 contact_entry 对象中的联系人增加。在循环之前创建该对象。

于 2012-05-31T07:33:57.737 回答
0

我想你不想做的是限制可以添加的联系人数量。

现在您将 size 声明为 contact_entry 的实例变量。为了让它工作,它应该是一个类变量(以便每个对象都相同)。

但请不要这样做,因为它只是简单丑陋的设计。

你可以做的是在你的诺基亚上做一个收藏,并对其进行一些限制。

每次创建新的联系人条目时,首先检查集合是否已满,如果没有,则将其添加到集合中。

于 2012-05-31T07:33:14.170 回答
0

The value is being set to 1, however the object obj is going out of scope, as soon as it leaves the case statement, so it would be reset to "2" once you create a new obj, as per the default value, in the class itself.

于 2012-05-31T07:41:15.663 回答