Java 需要将未指定的数字读入数组

发布于 2024-12-06 17:24:21 字数 1141 浏览 0 评论 0原文

好的,所以我需要根据我在程序的另一部分中指定的数字用整数填充数组。这就是我到目前为止所得到的:

public abstract class Polygon implements Shape {


      //numSides is the number of sides in a polygon 

        int numSides; 

          //Vertices[] is the array of vertices, listed in counter-clockwise sense 

        Point[] Vertices; 
        public Polygon(Point[] Vertices){ 
                //THIS IS WHERE I NEED THE HELP, DONT KNOW WHAT TO PUT IN HERE  
            }


       public Point getPosition(){ 
           return this.Vertices[0]; 
                }

       }

提前谢谢您。

抱歉缺少信息。是的,这是一个班级。我可以看出 ArrayList 可能会更好。该程序本身有一个接口 Shape,它由类 Point 和类 Polygon 实现,它们有一个扩展该接口的类,即矩形。这个想法是将顶点数量放入一个数组中,并返回 Vertices[0] 处的值作为多边形的位置。此类的其余部分如下所示:

    public abstract class Polygon implements Shape {

       //numSides is the number of sides in a polygon 

        int numSides; 
        Point[] Vertices; 
        public Polygon(Point[] Vertices){ 
            //your code
            }


        public Point getPosition(){ 
          return this.Vertices[0];  
         } 
        }

不确定您是否需要查看程序的其余部分。 再次感谢您。

ok so i need to fill an array with integers based on a number that i specify in another part of the program. This is what I have so far:

public abstract class Polygon implements Shape {


      //numSides is the number of sides in a polygon 

        int numSides; 

          //Vertices[] is the array of vertices, listed in counter-clockwise sense 

        Point[] Vertices; 
        public Polygon(Point[] Vertices){ 
                //THIS IS WHERE I NEED THE HELP, DONT KNOW WHAT TO PUT IN HERE  
            }


       public Point getPosition(){ 
           return this.Vertices[0]; 
                }

       }

Thank you in advanced.

Sorry for the lack of information. Yes this is for a class. I can see how an ArrayList would probably be better. The program its self has an interface, Shape, which gets implemented by a class Point and a class Polygon, which them has a class that extends that, rectangle. The idea is to put the number of vertices into an array and return the value at Vertices[0] as the position of the polygon. The rest of this class looks like this:

    public abstract class Polygon implements Shape {

       //numSides is the number of sides in a polygon 

        int numSides; 
        Point[] Vertices; 
        public Polygon(Point[] Vertices){ 
            //your code
            }


        public Point getPosition(){ 
          return this.Vertices[0];  
         } 
        }

Not sure if you need to see the rest of the program.
thank you again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-12-13 17:24:21

您不太清楚您需要什么,但我想您需要有关构造函数的帮助:

public Polygon(Point[] Vertices){ 
    this.Vertices = Vertices;
    this.numSides = Vertices.length;
}

You are not being very clear in what you need, but I guess you want help on your constructor:

public Polygon(Point[] Vertices){ 
    this.Vertices = Vertices;
    this.numSides = Vertices.length;
}
沐歌 2024-12-13 17:24:21

也许这会引导您走向正确的方向:

public static void main(String[] args) {
    System.out.print("how many? ");
    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    int[] numbers = new int[size];
    System.out.println("Enter the " + size + " numbers.");
    for (int i = 0; i < size; i++) {
        System.out.print(" " + (i + 1) + ": ");
        numbers[i] = in.nextInt();
    }
    System.out.println("Numbers entered: ");
    for (int number : numbers) {
        System.out.print(number);
        System.out.print(' ');
    }
}

正如其他人所说,问题中的更多细节将为答案带来更详细的信息。而且,这闻起来像家庭作业。如果是这样,您应该这样标记它。

Maybe this will guide you in the right direction:

public static void main(String[] args) {
    System.out.print("how many? ");
    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    int[] numbers = new int[size];
    System.out.println("Enter the " + size + " numbers.");
    for (int i = 0; i < size; i++) {
        System.out.print(" " + (i + 1) + ": ");
        numbers[i] = in.nextInt();
    }
    System.out.println("Numbers entered: ");
    for (int number : numbers) {
        System.out.print(number);
        System.out.print(' ');
    }
}

As the others have said, more detail in the question will bring better detail in answers. Also, this smells like homework. If so, you should tag it as such.

冰雪之触 2024-12-13 17:24:21

由于您的问题不清楚,我假设您只想用随机整数填充数组。
首先,您需要指定数组的大小,而执行此操作的最佳位置是您的构造函数。

 public Polygon(Point[] Vertices){ 
        Vertices = new Point[i];
       //based on a number i specified in another part of the program
       // Now you can use the scanner class to fill your array, see Ryan Stewart's answer for details on using Scanner class.
    }

我希望这会有所帮助:)
干杯!!!

As your question is not clear I am assuming that you that you just want to fill your array with random integers.
Here first of all you need to specify size of the array, and the best place to do this is your constructor.

 public Polygon(Point[] Vertices){ 
        Vertices = new Point[i];
       //based on a number i specified in another part of the program
       // Now you can use the scanner class to fill your array, see Ryan Stewart's answer for details on using Scanner class.
    }

I hope this helps :)
cheers!!!

一杯敬自由 2024-12-13 17:24:21

我认为,您需要一个优雅的数组副本:

public Polygon(Point[] Vertices)
{ 
    if (Vertices == null || Vertices.length == 0)
    {
        return;
    }

    int i = Vertices.length;
    this.Vertices = new Point[i];
    System.arraycopy(Vertices, 0, this.Vertices, 0, i);
}

之后,您可以迭代数组:

    ...
    for (Point point : this.Vertices)
    {
        // Use point
    }
    ...

I think, you need an elegant array copy:

public Polygon(Point[] Vertices)
{ 
    if (Vertices == null || Vertices.length == 0)
    {
        return;
    }

    int i = Vertices.length;
    this.Vertices = new Point[i];
    System.arraycopy(Vertices, 0, this.Vertices, 0, i);
}

After, you can iterate over your array:

    ...
    for (Point point : this.Vertices)
    {
        // Use point
    }
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文