如果一个数字不是素数,那么我们必须打印出可被整除的数字

时间:2016-12-06 19:50:27

标签: java

import java.util.Scanner;
public class exam2015q2
{
    public static void main (String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Eneter a number");
        int num = scan.nextInt();
        int num1 = num-1;
        int count = 0;

        if(num % 2 == 0)
        {

            count++;

            System.out.println(+num+ " Is an even number");
        }
        else
        {
            System.out.println(num+" Is an odd number");
        }

        boolean isPrime = true;

        do {

            if(num % num1 == 0)
            {
                isPrime = false;
                break;
            }
            num1--;
        }
        while(num1 >= 2);
        if(isPrime == true)
        {
            System.out.println(num +" is a prime number  as it is only divisible by 1 and " +num);
        }
        else
        {
            System.out.println(num +" is NOT a prime number.It is divisible by " +count);

        }
    }
}

在这个我需要输出如果它不是素数然后我们必须输出它可被整除的数字但我不能得到它打印出来。我需要帮助,有人可以给我建议如何获取可被

分割的数字

2 个答案:

答案 0 :(得分:1)

public static void main (String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Eneter a number");
        int divisibleBy;
        int num = scan.nextInt();
        int num1 = num-1;
        int count=0;

        if(num%2==0)
        {
            count++;
            System.out.println(+num+ " Is an even number");
        }
        else
        {
            System.out.println(num+" Is an odd number");
        }
        boolean isPrime = true;
        do{
            if(num%num1==0)
            {
                divisibleBy = num1;
                isPrime = false;
                break;
            }
            num1--;
        }
        while(num1>=2);
        if(isPrime == true)
        {
               System.out.println(num +" is a prime number  as it is only divisible by 1 and " +num);
        }
        else
        {
            System.out.println(num +" is NOT a prime number.It is divisible by " +divisibleBy);



 }
}

答案 1 :(得分:0)

只需更改显示变量的语句,该变量可以从countnum1整除,因为您在do循环中确定num可以除以{已经{1}}。

num1

您可以在程序中完全删除else{ System.out.println(num +" is NOT a prime number.It is divisible by " +num1); }