First clue to you is to use 'break' and not 'return'
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1
Here is the completed code I worked out. Note - I expanded to a long to enable you to enter large numbers.
import java.io.*;
public class PrimalityTest {
public static void main(String[] args) throws IOException {
// create a keyboard input stream
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
// variables to store input
// int Input;
long Input;
// Added boolean flag
boolean isprime;
do {
// prompt for user input
System.out.print("Please enter a number: ");
// Input = Integer.parseInt(stdin.readLine());
Input = Long.parseLong(stdin.readLine());
isprime = true; // Set/reset flag
// test for prime number
for (long i = 2; i < Input; i++) {
if ((Input % i) == 0) {
System.out.println(Input + " is not a prime number");
System.out.println("It is divisable by: " + i);
isprime = false;
break; // Break out of loop
}
}
// See if we found a divisor
if(isprime){
System.out.println(Input + " is a prime number");
}
// if number is 0 then exit
if (Input == 0) {
System.out.println("Exiting program...");
} else
// if number is negative
if (Input <= -1) {
System.out.println("Please enter a positive integer.");
}
} while (Input != 0);
}
}
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1