public class Looping {
public static void main(String[] args) {
int arr[] = { 11, 21, 31, 41, 51, 61, 71, 81, 91, 12, 22, 32, 42, 52, 62, 72, 82, 92 };
for (int i = 1; i <= 100; i ) {
if (i == arr[0] | i == arr[1] | i == arr[2] | i == arr[3] | i == arr[4] | i == arr[5] | i == arr[6]
| i == arr[7] | i == arr[8] | i == arr[9] | i == arr[10] | i == arr[11] | i == arr[12]
| i == arr[13] | i == arr[14] | i == arr[15] | i == arr[16] | i == arr[17]) {
continue;
}
System.out.println(i);
}
}
}
Want output is 1-100 expect=without array number
CodePudding user response:
When you use relational databases you reason in terms of Set Theory from mathematics. You can solve this in a similar way with a a Set Difference:
Set<Integer> toBeExcluded = new HashSet<>();
int value = 11;
for (int i = 0; i <= 17; i ) {//stepping in 11,12,21...
toBeExcluded.add(value); toBeExcluded.add( value);
value = 9;
}
Set<Integer> oneHundred = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toSet());
Set<Integer> difference = new HashSet<>(oneHundred);
difference.removeAll(toBeExcluded);
for (Integer integer : difference) {
System.out.print(integer " ");
}
CodePudding user response:
The following soulution is using a Set factory method which needs Java 9 or newer version. https://openjdk.org/jeps/269
public static void main(String[] args) {
Set<Integer> doNotPrintNumbers = Set.of(11, 21, 31, 41, 51, 61, 71, 81, 91, 12, 22, 32, 42, 52, 62, 72, 82, 92);
for (int i = 1; i <= 100; i ) {
if (!doNotPrintNumbers.contains(i)) {
System.out.println(i);
}
}
}
Or by using streams:
public static void main(String[] args) {
Set<Integer> doNotPrintNumbers = Set.of(11, 21, 31, 41, 51, 61, 71, 81, 91, 12, 22, 32, 42, 52, 62, 72, 82, 92);
IntStream.rangeClosed(1, 100)
.filter(n -> !doNotPrintNumbers.contains(n))
.forEach(System.out::println);
}