我应该如何重写这个程序的代码



程序的功能是让用户知道一个数字是否为素数。代码编译了,但字符串没有打印出来,我无法输入任何用户输入。

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=0;
int i=2;

System.out.print("Which number would you like to check?");
num = sc.nextInt();

while (i<=num) {
if (i ==num || num==1||num==0)  {
System.out.print("Is not prime");
}
else if (num%i==0){
System.out.print("Is prime");
}
else {
++i;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=0;
int c=0;  //A counter to store the number of factors

System.out.print("Which number would you like to check?");
num = sc.nextInt();
for(int i=1;i<=num/2;i++){
if(num % i==0)
c++;
}
if(c==1)
System.out.print("Prime");
else
System.out.print("Not Prime");
}

相关内容

  • 没有找到相关文章

最新更新