2016-11-27 53 views
1

它我期待输入10和100之间的整数为一个维阵列,并且如果该值已经在阵列中任何地方存在,不将它插入到数组中,但通知用户并恢复输入,直到添加5个唯一号码。搜索在数组的值,并存储它,如果不存在

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

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

回答

0

您正在将输入直接保存在数组中。
将输入保存在您将传递至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--; 
    } 
} 
1

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

的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:使用你的方法与搜索()方法

+0

谢谢,它的一些有点高于我的头,但我相信我可以通过这些工作,其中的答案就在于此。非常感激! – becs

+0

不客气。如果您有任何问题,请随时提问。注意:在示例代码的第三块(使用搜索方法)中,为了使for循环保持到使用5个数字,使用'i - ;'来重置/去除'i' int变量1 。如果在没有输入数字时没有使用'i - ;',那么for循环将在5次迭代后停止,无论如何(例如,输入的数字不正确,但程序停止查找) –

+0

其中正是我出错的地方。我今天正在研究这些代码,并且能够发挥出色,感谢您的帮助。我一直在增加柜台而不是减少柜台。 – becs

相关问题