有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

需要帮助为这个Java程序开发一个合适的打印方法吗

这个程序从用户输入中获取整数,并将它们放入一个集合中。然后它先打印正值,然后打印负值,不打印重复的数字。一旦用户输入0,它就会停止请求输入。以下是代码:

public class Intcoll2
{
   private int[] c;
   private int[] d;
   private int howmany = 0;

   public Intcoll2()
   {
        c = new int[500];
   }

   public Intcoll2(int i)
   {
        c = new int[i]
   }

   public void insert(int i)
   {
        if (i > 0)
        {
             int j = 0;
             while ((j <= howmany) && (c[j] != i)) j++;
             if (j == howmany)
             {
                   if (j == c.length - 1)
                   {
                        d = new int[2*c.length];
                        for (int k = 0; k<c.length; i++){
                            d[k] = c[k];
                        }
                        c = d;
                   }
                   c[j] = i; c[j + 1] = 0;
             }
             howmany++;
         }
    }

    public int get_howmany()
    {
          int j=0, howmany=0;

          while (c[j]!=0) {howmany++; j++;}
          return howmany;
    }

现在,我当前的打印方法如下所示:

   public void print()
   {
        int j = 0;
        System.out.println();
        while (j <= howmany)
        {
            System.out.println(c[j]); j++;
        }
   }

但当我尝试在我的客户机中使用它时,它只打印出零。对于我所做的错事,任何帮助都将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    我看了一下你们的课,用更清晰的方式重写了一遍。我没有测试它,但我相信它能工作。你可以查看一下,希望能了解发生了什么。希望这有帮助

    public class IntCollection2 {
    
        private int[] collection; // A large allocation, not neccessarily filled up.
        private int currentSize; // The number of spots currently filled in the collection.
    
        public IntCollection2() {
    
            collection = new int[500];
            currentSize = 0;
    
        }
    
        public IntCollection2(int size) {
    
            collection = new int[size];
            currentSize = 0;
    
        }
    
        /**
         * Inserts a new element into the internal array. If the current array is filled up,
         * a new array double the size of the current one is allocated.
         * @param element An int to insert into the collection. Must not be '0'.
         * @return True if the element was successfully inserted, false if the element was
         *         equal to '0' and was ignored.
         */
        public boolean insert(int element) {
    
            if (element != 0) {
    
                if (currentSize < collection.length - 1) {
    
                    collection[currentSize] = element;
    
                } else {
    
                    int[] newCollection = new int[collection.length * 2];
    
                    for (int i = 0; i < currentSize; i++) {
    
                        newCollection[i] = collection[i];
    
                    }
    
                    newCollection[currentSize] = element;
                    collection = newCollection;
    
                }
    
                currentSize++;
                return true;
    
            } else {
    
                return false;
    
            }
    
        }
    
        /**
         * Not actually necessary because the class automatically updates its currentSize
         * every time a new element is inserted.
         * @return The current number of filled spots in the internal array.
         */
        public int getCurrentSize() {
    
            int size = 0;
    
            for (int i = 0; i < collection.length && collection[i] != 0; i++) {
                size++;
            }
    
            return size;
    
        }
    
        /**
         * Prints out all the elements currently in the collection, one on each line.
         */
        public void print() {
    
            System.out.println();
    
            for (int i = 0; i < currentSize; i++) {
    
                System.out.println(collection[i]);
    
            }
    
        }
    
    }
    

    仅供参考:这个类只是按顺序打印出集合中的每个元素。你提到了打印正值然后是负值,但我把这个留给你

    编辑:我猜你是编程新手,所以我只想明确什么是集合。数组是元素的有序列表。当你创建一个数组时,计算机会留出一点内存来精确保存你告诉它的元素数。不能更改现有阵列的大小。集合基本上是数组的包装器。它制作了一个比容纳其元素所需的更大的数组,当其数组变满时,它会分配一个新的、更大的数组来容纳更多元素

  2. # 2 楼答案

    一个你可能不在寻找的答案,但仍然是你唯一应该关心的真正的答案

    你的问题不是代码的某个地方隐藏着一个bug。问题是,您的代码令人困惑超出了限制:

    • 不要使用单字符变量名
    • 接受int的构造函数。。。创建一个空的数组
    • 使用数组时不要说“collection”
    • 不要给字段和局部变量指定相同的名称

    严肃地说:理解这种混乱主要是复杂和困难的,因为你写的代码很难阅读

    现在你要求其他人调试如此复杂的代码,以至于你(创建它的作者!)一开始我不明白

    相反,你可能会把这整件事都扔掉。然后慢慢地再写一遍;但在某种程度上,读者一点也不感到困惑