Last Updated: July 04, 2019
· georgealie

Java Programming, Tip for Code?

ok basically im creating a RSA encryption program for my CS class, My java teacher seems to think that the "break" command is of the devil... how can i change this code ive created to not use it?.. ive tried alot of things but this is the only one that actually works...

basic idea is to find a random number and check if its prime.. if its not prime grab a new random number and check if thats a prime.. i then need to be able to use it if its prime.. heres what i have

int p = (int)(Math.random() * 128);

boolean pIsPrime = false;

while (pIsPrime == false) {

p = (int)(Math.random() * 128);

for (int i = 2; (i < p) || (pIsPrime == false); i++) {
if ((p%i==0) && (i<p/2)) {
System.out.println(" p not a prime");
} else {
if (p/i + 1==1) {
pIsPrime = true;
i -= 1;
System.out.println( i + " p is a prime");