这里的问题是,当用户输入'-1'时,我们希望程序停止循环/停止询问整数,它不必获得我们阵列的最大长度停止询问用户输入的循环
import java.util.Scanner;
public class DELETE DUPLICATES {
public static void main(String[] args) {
UserInput();
getCopies(maxInput);
removeDuplicates(maxInput);
}
static int[] maxInput= new int[20];
static int[] copies = new int[20];
static int[] duplicate = new int[20];
//get user's input/s
public static void UserInput() {
Scanner scan = new Scanner(System.in);
int integer = 0;
int i = 0;
System.out.println("Enter Numbers: ");
while(i < maxInput.length)
{
integer = scan.nextInt();
maxInput[i] = integer;
i++;
if (integer == -1)
break;
}
int j = 0;
for(int allInteger : maxInput) {
System.out.print(allInteger+ " ");
j++;
}
}
//to get value/number of copies of the duplicate number/s
public static void getCopies(int[] Array) {
for(int allCopies : Array) {
copies[allCopies]++;
}
for(int k = 0; k < copies.length; k++) {
if(copies[k] > 1) {
System.out.print("\nValue " + k + " : " + copies[k] + " copies are detected");
}
}
}
//remove duplicates
public static void removeDuplicates(int[] Array) {
for(int removeCopies : Array) {
duplicate[removeCopies]++;
}
for(int a = 0; a < duplicate.length; a++) {
if(duplicate[a] >= 1) {
System.out.print("\n"+a);
}
}
}
}
示例: 如果我们输入: -1
The result of our program is : 1 2 3 3 4 5 -1 0 0 0 0 0 0 0 0 0 0 0 0 0
We want the result to be like: 1 2 3 3 4 5
我们需要你的帮助guyz。练我们的节目1主题,希望我们能找到这里一些帮助
阵列具有固定的大小。如果您想要一个可以更改大小的列表,请尝试列表。 – 2013-03-14 05:35:39
第一课:Java不是JavaScript。 – 2013-03-14 05:35:43
'公共类DELETE DUPLICATES'这不会编译!也遵循Java第一个字母大写的信头不是全部 – 2013-03-14 07:16:07