-7
我編寫了一個代碼來標識素數。沒有語法錯誤,但是當它不是素數時,它會再次循環輸入正整數。它應該是「再次嘗試按1並退出按2」。JAVA:標識素數
這裏是我下面的代碼:
import java.util.*;
public class TRY
{
static Scanner Scan = new Scanner(System.in);
public static void main (String[] arg)
{
int n;
System.out.println("Identify the if it's a prime number");
while (true){
System.out.println();
System.out.println("Enter a positive integer: ");
n = Scan.nextInt();
if (n>0)
{
boolean isPrime = true;
for (int i = 2; i <= n/2;i++)
{
if (n % i == 0){
isPrime = false;
}
}
if (isPrime){
System.out.println("The integer you entered " + n + " is a PRIME NUMBER!");
}
else
{
System.out.println("The integer you entered " + n + " is not a PRIME NUMBER!");
continue;
}
System.out.println();
System.out.println("To try again press 1 and to exit press 2");
if (Scan.nextInt() == 1)
{
continue;
}
else
{
System.out.println("Thank you!");
break;
}
}
}
}
}
歡迎來到SO。你可能想知道爲什麼有人低估你的問題......請看看這裏:[問] – Fildor 2014-11-25 06:43:24
提出一個合適的標題。沒有人會谷歌_「我需要一點幫助,請」_ – Baby 2014-11-25 06:49:50
對不起,我不會再那樣做。 – anomalyyaboi 2014-11-25 06:50:29