**7.5 (Print different numbers) Write a program, read in 10 numbers, display the number of different numbers, and display these numbers in the order of input, separated by a space (that is, a number appears multiple times, also Only shown once). (Hint: read in a number, if it is a new number, store it in the array. If the number is already in the array, ignore it.) After input, the array contains all different numbers. The following is a running example of this program: Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2 The number of distinct numbers is 6 The distinct numbers are: 1 2 3 6 4 5 **7.5(Print different numbers)Write a program, read in 10 numbers, display the number of different numbers, and display these numbers in the order of input, separated by a space (that is, a number appears many times, but only once). (tip: read in a number and store it in the array if it is a new number. If the number is already in array China, ignore it.) After input, the array contains different numbers. Here is an example of how to run this program: Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2 The number of distinct numbers is 6 The distinct numbers are: 1 2 3 6 4 5
Reference Code:
package chapter07;import java.util.ArrayList;import java.util.List;import java.util.Scanner;publicclassCode_05{
publicstaticvoidmain(String[] args){
List<Integer> list =newArrayList<Integer>();
Scanner input =newScanner(System.in);
System.out.print("Enter 10 numbers: ");
String[] strings = input.nextLine().split(" ");int count =0;for(int i =0;i < strings.length;i++){
if(!list.contains(Integer.parseInt(strings[i]))){
list.add(Integer.parseInt(strings[i]));
count++;}}
System.out.println("The number of distinct numbers is "+ count);
System.out.print("The distinct numbers are: ");for(int i : list)
System.out.print(i +" ");}}
The results show that:
Enter 10 numbers:1232163452
The number of distinct numbers is 6
The distinct numbers are:123645
Process finished with exit code 0