Reputation: 308
The program just need to take a user input and say whether it's prime or not, then it should ask them if they want to do it again until they don't want to.
Here's what I have so far:
import java.util.*;
public class PrimeOrNot
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
int n = 0;
String reply;
char doAgain = 'Y';
boolean valid = false, isPrime = true;
Welcome();
do
{
System.out.print("Enter a whole number: ");
n = scan.nextInt();
if(PrimeTest(n))
System.out.println(n + " is a prime number!");
if(!PrimeTest(n))
System.out.println(n + " is not a prime number!");
//Asks the user to try again until they enter a valid
//response of Y or N
while(!valid)
{
System.out.print("Try another number? (Y/N): ");
reply = scan.next().toUpperCase();
doAgain = reply.charAt(0);
System.out.println();
if(doAgain == 'Y' || doAgain == 'N')
valid = true;
else
System.out.println("Please type 'Y' or 'N'.");
}
}while(doAgain != 'N');
Goodbye();
}
/*----------------------------------------------------------*/
/********************STATIC METHODS BELOW********************/
/*----------------------------------------------------------*/
/****************************************/
//Welcome method...a welcoming 'graphic'//
/****************************************/
public static void Welcome()
{
System.out.println("=============");
System.out.println("Prime or Not");
System.out.println("=============");
System.out.println();
}
/****************************************************/
//PrimeTest determines if the number is prime or not//
/****************************************************/
public static boolean PrimeTest(int n)
{
boolean isPrime = true;
if(n > 0)
{
for(int x = 2; x <= Math.sqrt(n); x++)
{
if(n % 2 == 0)
isPrime = false;
else if(n % x == 0)
isPrime = false;
else
isPrime = true;
}
}
else
System.out.println("Please enter a positive integer.");
return isPrime;
}
/****************************************/
//Goodbye method...a departing 'graphic'//
/****************************************/
public static void Goodbye()
{
System.out.println("===============");
System.out.println("Try again soon!");
System.out.println("===============");
System.out.println();
}
}
Here's what's happening. I'll put in a number, and it'll answer most things wrong. Then when I press Y to do another, it just keeps having me enter numbers and telling me if it's prime or not, and will never ask me if I actually want to keep going. I have absolutely no idea how to fix either issue.
Upvotes: 1
Views: 43
Reputation: 24427
Your prime test won't work because you keep testing even after you have recognized that it is not a prime. As soon as you find even one divisor then you should set isPrime
to false and break out of the loop. Don't set it to true on the next test! Also you don't need a separate test for n % 2 since you will test that when x = 2.
for(int x = 2; x <= Math.sqrt(n); x++)
{
if(n % x == 0)
{
isPrime = false;
break;
}
}
When you call the function, call it only once and save the result in a boolean:
boolean result = PrimeTest(n);
if(result)
System.out.println(n + " is a prime number!");
if(!result)
System.out.println(n + " is not a prime number!");
Your prompt isn't working because you don't reset valid to false before asking the second time:
valid = false;
while(!valid)
Upvotes: 3