簡體   English   中英

將信息存儲到另一個類的數組中

[英]Storing info into an array of another class

我已經為此工作了一段時間,但似乎無法理解。 我需要將用戶輸入內容從另一個對象存儲到數組中,但無法使其正常工作。 我不確定它是我的構造函數還是缺少什​​么,但是可以提供任何幫助

這是輸出程序

    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);

這是另一個類的構造函數

public class Polygon {
    protected int[] sideLengths;


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

}

你需要做

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;

}

通過這樣做,我們將用戶輸入的“邊”值分配給數組中的特定索引。

希望這可以幫助!

祝好運

我了解您的程序的方式我認為您正在尋找這種邏輯

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

        }

而您的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;
    }
}

現在,我們創建了一個邊長為大小的數組,並將該數組作為參數傳遞給構造函數以初始化Polygon的對象

它應該像

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);
}}}

和多邊形類一樣

public class Polygon {
    protected int[] sideLengths;


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

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

您正確地調用了構造函數,但是在for循環結束之后,您再次創建了Polygon的新實例並將其分配給ps。 創建一個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