【问题标题】:How do I make my array print repetitions of an input once? (JAVA)如何让我的数组打印输入重复一次? (JAVA)
【发布时间】:2018-12-17 00:31:45
【问题描述】:

我必须设计一个程序,接受 0-50 的任意输入打印所有 输入 ONCE,然后 打印每个输入的出现次数。

我在某种程度上可以正常工作,但是,当输入是: 1 , 2 , 3 , 3 , 3 , 6 , 9 , 0 , 0

打印出来:

输入:发生次数

     Number   Times
      1         1
      2         1
      3         1
      3         2
      3         3
      6         1
      9         1
      0         1
      0         1

代替:

输入:发生次数

     Number Times
       0    2
       1    1
       2    1
       3    3
       6    1
       9    1

这是一门初学者课程,我在网上看到的大多数解决方案似乎都是使用某种我尚未学过的映射技术进行的高级解决方案。

 public static void main(String [] args)
{

   int[] array = new int[51];
   Scanner scan = new Scanner(System.in);
   System.out.println("Number \t   Times");

   while (scan.hasNext()){    
    int x = scan.nextInt();
    if (x>=0 && x<=50){
        array[x]++;
  System.out.println(x + "\t      " + array[x]);
      }
    }
  }
}

我尝试了多种格式化循环的方法,但我似乎不知道如何让它打印一个多次输入的数字。

【问题讨论】:

  • 以及基本上有两部分数据很重要[数字和计数],那么您将需要使用 map 来使用数组(或列表)包含数字和count 的类

标签: java arrays logic system.printing


【解决方案1】:

欢迎来到 SO。解决这个问题的最简单方法是不使用地图,甚至不将值存储在任何地方,首先是对数组进行排序(您给出的示例已经排序),然后只计算相邻重复项的数量。

在伪代码中,算法应该类似于

count = 1
value = array[0];
for each item from 1 to length
    if item == value
        increment count
    else
        print value: count
        count = 1
        value = item
print value: count

请注意,需要有 2 个输出 - 每次值更改时以及在列表末尾。理想情况下,您会将值和计数存储在一个对象中以避免代码重复,但我认为现阶段这太先进了。

希望您可以相对轻松地将其转换为代码。

【讨论】:

  • 谢谢,我会试一试,看看效果如何。
  • @JontyMorris 和 Java 中仅有的 3-4 个单独的类!
【解决方案2】:

如果您还在寻找,这里是另一个答案。我会留下 hashmaps 的答案,因为其他人可能会觉得这很有用,但我决定让你当前的代码也能正常工作。

int[] numbers = new int[51];

// Small loop to get the number input
Scanner scanner = new Scanner(System.in);
for (int i=0; i<10; i++) {
    System.out.print("> ");
    int x = scanner.nextInt();

    if (x >= 0 && x <= 50) {
        numbers[x] += 1;
    }
}

// Now display the results after getting input
System.out.println("Number \t     Times");
for (int i=0; i<numbers.length; i++) {
    if (numbers[i] != 0) {
        System.out.println(i + "\t\t" + numbers[i]);
    }
}

【讨论】:

  • 顺便说一句,如果您支持对您有帮助的答案并在您最喜欢的答案上按接受按钮,每个人都会非常感激 ?
  • 感谢您的帮助。我已经对有帮助的适当答案投了赞成票,但由于我的声誉低下,它没有公开显示。当我让我的东西工作一点时,我很快就会接受答案。只需要重新格式化一些东西!
【解决方案3】:

欢迎来到 StackOverflow 社区!我知道您提到您还没有学习“高级映射技术”,但为什么不现在学习它们呢?无论如何,您将来很有可能再次需要它们。

我们可以通过使用称为“哈希图”的东西轻松解决这个问题。 hashmap 很有用,因为它允许您在每个索引处存储两个值,一个键和一个值。这很有用,因为 key 与 value 相关(这意味着如果你有 key,你可以找到 value),并且不能有重复的 key。

这是使用哈希图解决问题的示例。

// Here we create our hashmap. Be adding <Integer, Integer>, we are telling the hashmap
// that the the key and value will be of type Integer (note that we can't just pass in int)
HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();

Scanner scan = new Scanner(System.in);
System.out.println("Number \t   Times");

while (scan.hasNext()){    
  int x = scan.nextInt();
  if (x>=0 && x<=50){

      // Check if the number has already been added
      // to the hash map
      if (numbers.containsKey(x)) {
          // If so, we will get the existing value
          // and increase it by 1
          numbers.put(x, numbers.get(x) + 1);
      }

      else {
          // Otherwise we will add the value
          // to the hash map
          numbers.put(x, 1);
      }

      System.out.println(x + "\t      " + numbers.get(x));
  }
}

【讨论】:

  • 您好,感谢您的回复,但是,我不能使用哈希图,因为我们还没有学会这些。它似乎并不太复杂,但我宁愿坚持课堂上所教的内容。
  • 不用担心。我建议您主动学习一下哈希图,因为将来您很可能会再次需要它们(它甚至可能会给您的老师留下深刻印象?)
猜你喜欢
  • 2023-03-07
  • 2015-11-17
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2021-04-09
  • 2019-09-10
  • 1970-01-01
  • 2021-02-04
相关资源
最近更新 更多