【问题标题】:Searching for a value in an array, and storing it if it doesn't exist在数组中搜索一个值,如果它不存在则存储它
【发布时间】:2017-04-11 00:16:52
【问题描述】:

我希望将 10 到 100 之间的整数输入一维数组,如果该值已存在于数组中的任何位置,则不要将其插入数组中,而是通知用户并继续输入,直到 5 个唯一数字添加。

这是我的代码。我知道这是不对的,但是您可以看到我正在尝试做的是使用简单的 for 循环和搜索方法来获取数字,将它们存储到数组中并搜索重复项。我的代码中的问题是我似乎无法将刚刚输入的数字设置为我需要发送到方法“搜索”的变量“键”。

// input an integer between 10 and 100, add to array and print results. if value is already in array, notify user, print array. keep adding to array until 5 unique values have been entered

import java.util.Scanner;

public class ArraySearch {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int[] list = new int[5];

        for (int i = 0; i < list.length; i++) {
            System.out.println("Enter number: ");
            list[i] = input.nextInt();
        }
        int count = search(list, key);
        System.out.println("It has been entered.");
    }

    public static int search(int[] list, int key) {

        int count = 0;
        for (int i = 0; i < list.length; i++) {
            if (list[i].equals(key)) {
                ;
            }
            count++;
        }
        return (count);
    }
}

【问题讨论】:

    标签: java arrays loops conditional


    【解决方案1】:

    您将输入直接保存在数组中。
    将输入保存在您将传递给search 的时间变量中。并根据search 的结果添加到数组或提示输入另一个输入。

    int[] list = new int[5];
    
    for (int i = 0; i < list.length; i++) {
        System.out.println("Enter number: ");
        int temp = input.nextInt();
        if(search(list,temp) == 0)
            list[i] = temp;
        }else{
            System.out.println("It has been entered.");
            i--;
        }
    }
    

    【讨论】:

      【解决方案2】:

      带有数组的简单示例。可以通过备用数据结构列表集进行改进。

      search() 方法本质上包含在 while() 循环中,即 for() 循环示例搜索已包含的目标数。

      int c = 0; 在循环之前声明并确保找到 5 个唯一数字。

      import java.util.Arrays;
      import java.util.Scanner;
      
      public class ArraySearch {
          public static void main(String[] args) {
              Scanner s = new Scanner(System.in);
              int[] list = new int[5];
              int c = 0;
              System.out.println("Enter number: ");
              while (c < 5 && s.hasNext()) {
                  int n = s.nextInt();
                  boolean has = n >= 10 && n <= 100;
                  for (int i = 0; i <= c && !has; ++i)
                      if (list[i] == n)
                          has = true;
                  if (!has) {
                      System.out.println("It has been entered.");
                      list[c++] = n;
                  }
              }
              System.out.println("Result = " + Arrays.toString(list));
              s.close();
          }
      }
      

      替代版本:

      import java.util.HashSet;
      import java.util.Scanner;
      import java.util.Set;
      
      public class ArraySearch {
          public static void main(String[] args) {
              Scanner s = new Scanner(System.in);
              Set<Integer> set = new HashSet<Integer>(5);
              int c = 0;
              System.out.println("Enter number: ");
              while (c < 5 && s.hasNext()) {
                  int n = s.nextInt();
                  if ((n < 10) || (n > 100) || !set.add(n))
                      continue;
                  else {
                      System.out.println("It has been entered.");
                      c++;
                  }
              }
              System.out.println("Result = " + set);
              s.close();
          }
      }
      

      另外,使用search()

      public class App {
          public static void main(String[] args) {
              Scanner s = new Scanner(System.in);
              int[] list = new int[5];
              for (int i = 0; i < 5; i++) {
                  System.out.println("Enter number: ");
                  int n = s.nextInt();
                  if ((n >= 10 && n <= 100) && search(list, n) == 0) {
                      list[i] = n;
                      System.out.println("It has been entered.");
                  } else
                      i--;
              }
              System.out.println("Result = " + Arrays.toString(list));
              s.close();
          }
      
          public static int search(int[] list, int key) {
              int count = 0;
              for (int i = 0; i < list.length; i++) {
                  if (list[i] == key) {
                      count++;
                  }
              }
              return count;
          }
      }
      

      编辑:还添加了 10-100 规格

      edit2:将您的方法与 search() 方法一起使用

      【讨论】:

      • 谢谢你,其中一些有点超出我的想象,但我相信我可以解决这些问题,这就是答案。非常感谢!
      • 不客气。如果您有任何问题,也请随时提出。注意:在示例代码的第三块(使用搜索方法)中,i--; 用于将i int 变量重置/递减 1,以保持 for 循环继续运行,直到使用 5 个数字。如果在未输入数字时未使用 i--;,则无论如何 for 循环将在 5 次迭代后停止(例如,无法正确输入 5 个数字但程序将停止查找)
      • 这正是我出错的地方。我今天正在编写代码并且能够将其打出来,感谢您的帮助。我一直在递增计数器而不是递减它。
      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2014-07-20
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      相关资源
      最近更新 更多