简体   繁体   English

将信息存储到另一个类的数组中

[英]Storing info into an array of another class

I've been working on this for a while but can't seem to get it. 我已经为此工作了一段时间,但似乎无法理解。 I need to store user input into an array from another object but I can't get it to work. 我需要将用户输入内容从另一个对象存储到数组中,但无法使其正常工作。 I'm not sure if its my constructor or I'm missing something but any help is appreciated 我不确定它是我的构造函数还是缺少什​​么,但是可以提供任何帮助

Here is the output program 这是输出程序

    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter   'p' to create a polygon");
       String in = sc.next();

    if (in.equals("p")) {
        System.out.println("How many sides?");
        int numSides = sc.nextInt();
        int side=0; 

        Polygon ps;
        for (int i = 1; i <= numSides; i++) {

            System.out.println("Enter the length of side " + i);
            side = sc.nextInt();
             ps = new Polygon(side);

        }
         ps = new Polygon(side);

Here is the constructor of the other class 这是另一个类的构造函数

public class Polygon {
    protected int[] sideLengths;


public Polygon(int sides){
    sideLengths= new int[sides];

}

you need to do 你需要做

ps=new Polygon(numSides+1); //adding 1 because for-loop starts with index 1
for (int i = 1; i <= numSides; i++) {

      System.out.println("Enter the length of side " + i);
      side = sc.nextInt();
      ps[i]=side;

}

by this what we do is assign the 'side' value entered by user to a particular index in array. 通过这样做,我们将用户输入的“边”值分配给数组中的特定索引。

hope this helps! 希望这可以帮助!

Good luck 祝好运

The way I understand your Program I think you are looking for this kind of logic 我了解您的程序的方式我认为您正在寻找这种逻辑

if (in.equals("p")) {
            System.out.println("How many sides?");
            int numSides = sc.nextInt();
            int side = 0;

            Polygon ps;
            int sideLengths[] = new int[numSides];// array to store length of  polygon of sides `numSides`
            for (int i = 1; i <= numSides; i++) {

                System.out.println("Enter the length of side " + i);
              sideLengths[i-1] = sc.nextInt();// creating array to store lengths


            }
            ps = new Polygon(sideLengths);// generating your pollygon by sending the array

        }

And you Polygon class should look like this 而您的Polygon类应该看起来像这样

    class Polygon {

            protected int[] sideLengths;// storing the lengths of all sides

            public Polygon(int dimensions[]) {
                sideLengths = dimensions ;

            }
        }
 public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter   'p' to create a polygon");
       String in = sc.next();


            if(in.equalsIgnoreCase("p")) {
                Integer sides[];
                System.out.println("How many sides?");
                int numSides = sc.nextInt();
                int side=0; 
                if(numSides>0){
                    sides = new Integer[numSides];
                }
                Polygon ps;
                for (int i = 1; i <= numSides; i++) {

                    System.out.println("Enter the length of side " + i);
                    side = sc.nextInt();
                    sides[i] = side;

                }

                ps = new Polygon(sides);
            }
     }

public class Polygon {
    private Integer[] sides;
    public Polygon(Integer[] sides){
        this.sides = sides;
    }
}

Now we have created an array of size of sides and array is been passed as an argument to constructor for initializing Polygon's object 现在,我们创建了一个边长为大小的数组,并将该数组作为参数传递给构造函数以初始化Polygon的对象

it should be like 它应该像

public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter   'p' to create a polygon");
   String in = sc.next();

if (in.equals("p")) {
    System.out.println("How many sides?");
    int numSides = sc.nextInt();
    int side=0; 

    Polygon ps = new Polygon(numSides);
    for (int i = 1; i <= numSides; i++) {

        System.out.println("Enter the length of side " + i);
        side = sc.nextInt();
        ps.addSide(side, i-1);
         //ps = new Polygon(side);

    }
     //ps = new Polygon(side);
}}}

and Polygon class like 和多边形类一样

public class Polygon {
    protected int[] sideLengths;


    public Polygon(int sides){
        sideLengths= new int[sides];

    }
    public void addSide(int side, int index){
        sideLengths[index] = side;
    }
}

You are calling the constructor correctly, but after for loop ends you are again creating a new instance of Polygon and assigning it to ps. 您正确地调用了构造函数,但是在for循环结束之后,您再次创建了Polygon的新实例并将其分配给ps。 Either create an array of int or ArrayList of Integer, and store each instance of Polygon side length in that just before the for loop ends. 创建一个int数组或Integer的ArrayList,并在for循环结束之前在其中存储每个Polygon边长的实例。

ArrayList<int> psListpsSideLengthList = new ArrayList<int>;
for (int i = 1; i <= numSides; i++) {

    System.out.println("Enter the length of side " + i);
    side = sc.nextInt();
     psListpsSideLengthList.add(side);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM