【问题标题】:array in object oriented programming面向对象编程中的数组
【发布时间】:2018-10-12 10:22:15
【问题描述】:

我是面向对象编程的初学者,这是我的第一个小项目。我听说在这里每个人都可以帮助你编写代码,这是我的第一次。无论如何,我的问题为什么数组不存储任何价值?

代码如下:

    public class Information {
    private IT_Members[] member= new IT_Members[10];
    private int counter = 0;



Information()
{
    for ( int ctr=0;ctr<member.length;ctr++)
    {
        member[ctr] = new IT_Members ();

    }

}

public void Add(IT_Members member)
    {
        if(counter<10)
        {
        this.member[counter].setName(member.getName());
        this.member[counter].setDeparment(member.getDeparment());
        this.member[counter].setPostion(member.getPostion());
        this.member[counter].setID(member.getID()+counter);
        counter++;
        }
        else
            System.out.println("Add List Full"); 
    }
public void Display()
    {
        if (counter!=0)
        {
            for (int ctr=0;ctr<10;ctr++){
            System.out.println(this.member[ctr].getName()+
           this.member[ctr].getDeparment()+
           this.member[ctr].getPostion()+ 
           this.member[ctr].getID());


            }
        }
        else
        System.out.println("No member yet!");



    }

这是主类:

import java.util.Scanner;
import java.util.Arrays;
public class Interface {

    public static void main(String[]args)
    {
        Scanner in=new Scanner(System.in);
        IT_Members input1 = new IT_Members();
        Information input2 = new Information();
        int x=1;
        while(x!=0)
        {

        System.out.println(" \n[1] Add new student member. \n[2] View members.\nChoose now: ");
        int choose = in.nextInt();

        switch (choose){

            case 1:
              System.out.println("Name: ");
              input1.setName(in.nextLine());
              System.out.println("Deparment: ");
              input1.setDeparment(in.nextLine());
              System.out.println("Postion: ");
              input1.setPostion(in.nextLine());
              System.out.println("Student record has been added. ");
              break;

            case 2:

                input2.Display();
                break;

}
}

.................................................. …………

public class IT_Members {
    private String name,deparment,postion;
    private int ID=1000;
    private int Flag=0;

    IT_Members (){


    }
    IT_Members (String name, String deparment , String postion ,int ID , int Flag){
     this.name= name;
     this.deparment=deparment;
     this.postion=postion;
     this.ID=ID;
     this.Flag=Flag;


    }

    public String getName (){

        return this.name;
    }
    public String getDeparment (){

        return this.deparment;
    }

    public String getPostion (){

        return this.postion;
    }

    public int getID (){

        return this.ID;
    }

    public int getFlag (){

        return this.Flag;
    }

    public void setName (String name){

        this.name = name;
    }
    public void setDeparment (String Deparment){

        this.deparment = deparment;
    }

    public void setPostion (String postion){

        this.postion = postion;
    }

    public void setID (int ID){

        this.ID = ID;
    }

    public void setFlag (int Flag){

        this.Flag = Flag ;
    }

    public String toStu()
    {

        String str = "";
        str = "\nName: " + this.name +
                "\nDeparment: " + this.deparment + 
                "\nPostion: " + this.postion +
                "\nID: " + this.ID;
        return str;        
    }
}

拜托,我被困住了,感谢任何帮助。

谢谢。

【问题讨论】:

  • 代码的哪一行和哪一部分有问题?只提取有问题的代码并询问。
  • 在 case 1 的中断前插入 input2.Add(input1)

标签: java arrays oop methods


【解决方案1】:

您永远不会在 Information 类中调用 Add 函数。因此,您永远不会初始化任何您想要显示的数组元素。

您需要在打印之前添加input2.Add(input1) 已添加。

【讨论】:

    【解决方案2】:

    你必须每次创建一个新对象,最后你必须添加到列表中。

    import java.util.Scanner;
    import java.util.Arrays;
    public class Interface {
    
        public static void main(String[]args)
        {
            Scanner in=new Scanner(System.in);
            Information input2 = new Information();
            int x=1;
            while(x!=0)
            {
    
            System.out.println(" \n[1] Add new student member. \n[2] View members.\nChoose now: ");
            int choose = in.nextInt();
    
            switch (choose){
    
                case 1:
                  IT_Members input1 = new IT_Members();// this need to be here so that every time crete new object
                  System.out.println("Name: ");
                  input1.setName(in.nextLine());
                  System.out.println("Deparment: ");
                  input1.setDeparment(in.nextLine());
                  System.out.println("Postion: ");
                  input1.setPostion(in.nextLine());
                  input2.Add(input1); // that was missing
                  System.out.println("Student record has been added. ");
                  break;
    
                case 2:
    
                    input2.Display();
                    break;
    
    }
    }
    

    【讨论】:

    • 小错误毁了一切 :( 非常感谢。
    • 有时会发生 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2013-12-09
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多