-3

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

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 2
    Use another loop on your `arr[]`? – Thum Choon Tat Sep 29 '22 at 04:16
  • Does this answer your question? [Java, Simplified check if int array contains int](https://stackoverflow.com/questions/12020361/java-simplified-check-if-int-array-contains-int) – Nick Sep 29 '22 at 04:17
  • All the numbers you want to skip end in 1 or 2 and are at least 10. Do you know how to test what the last digit of a number is? If so, you can just check directly whether to print the number. – templatetypedef Sep 29 '22 at 04:39

2 Answers2

0

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 = Set.of(11, 21, 31, 41, 51, 61, 71, 81, 91, 12, 22, 32, 42, 52, 62, 72, 82, 92);
Set<Integer> oneHundred = IntStream.rangeClosed(1,100).boxed().collect(Collectors.toSet());
oneHundred.removeAll(toBeExcluded);// Set difference
System.out.println(oneHundred);
wromma
  • 21
  • 1
  • 5
-1

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);
   }
crs
  • 26
  • 4