【发布时间】:2015-05-03 13:12:25
【问题描述】:
问题正如标题所说,如何去除整数数组中的重复值?我想要它,所以用户输入五个数字,所有数字都在 10 到 100 之间。关键是我必须拥有它,这样如果他们输入的值已经输入到数组中,它就不会计数。这是我到目前为止的代码:
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int[] intArray = new int[5]; //max of 5 values
for(int i = 0; i < 5; i++){
System.out.println("Please enter your desired number that is between 10 and 100: ");
intArray[i] = input.nextInt(); //puts the value into the array
}
System.out.println(Arrays.toString(intArray)); //for test purposes to make sure the array was correctly taking in the values
}
}
System.out.println(Arrays.toString(intArray)); //for test purposes to make sure the array was correctly taking in the values
我对如何制作它感到困惑,所以如果用户输入数组中已经存在的数字,它就不会添加它。这是我的意思的一个例子,假设用户输入数字 15、22、46、46、77,一旦程序完成循环五次,它将打印出以下内容:[15、22、46、77]。我被困在如何做到这一点上。此外,我还编辑了 if 数字在 10 到 100 之间的 if 语句,以便于阅读,并进入手头的要点。
【问题讨论】:
标签: java arrays int duplicates duplicate-removal