Java

Random

Modulo

import java.util.Random;
public class Program {
public static void main(String[] args) {
Random random = new Random();
int count = 0;
*// Use while-loop until count reaches 10.*
while (count < 10) {
*// Get random number.*
*// ... Can be 0 through 9 inclusive.*
*// ... Bound argument (10) is exclusive.*
int value = random.nextInt(10);
*// Continue to next iteration if value is divisible by 2.*
if (value % 2 == 0) {
continue;
}
*// Check for 3.*
if (value % 3 == 0) {
continue;
}
*// Print result.*
System.out.println(*"Not divisible by 2 or 3: "* + value);
count++;
}
}
}Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 1
Not divisible by 2 or 3: 5
Not divisible by 2 or 3: 5
Not divisible by 2 or 3: 1
Not divisible by 2 or 3: 1
Not divisible by 2 or 3: 7
Not divisible by 2 or 3: 1

while

public class Program {
public static void main(String[] args) {
while (true) {
continue;
}
}
}

break

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Jan 24, 2024 (edit).