有一個程序,用戶在該數組中輸入10個int值。最後,我需要提取不同的值並顯示它們。增加了我的第二個循環,用於確定值是否不同(即表示該數字多次出現,只顯示一次)。例如,假設我傳入數字:1,2,3,2,1,6,3,4,5,2,不同的數組只應包含數字{1,2,3,4,5,2} 4,5}從java中的數組中提取不同的值
import java.util.Scanner;
import java.io.*;
public class ArrayDistinct {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
// Create arrays & variables
int arrayLength = 10;
int[] numbers = new int[arrayLength];
int[] distinctArray = new int[arrayLength];
int count = 0;
System.out.println("Program starting...");
System.out.print("Please enter in " + numbers.length + " numbers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
for (int i = 0; i < numbers.length; i++) {
int temp = numbers[i];
int tempTwo = numbers[i + 1];
if (tempTwo == temp) {
count++;
distinctArray[i] = temp;
}
}
// Print out results
} // end main
} // end class
什麼是 「獨特」 的價值觀?我認爲你是在假設我們有一些先前的知識。請更好地解釋你的問題。 – markspace 2014-10-31 17:02:58
考慮使用一套只允許唯一的值... – brso05 2014-10-31 17:03:03
@ brso05我猜它是作業;他還沒有設置。 – markspace 2014-10-31 17:04:10