java中的素数测试 [英] prime numbers test in java

查看:56
本文介绍了java中的素数测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务要求我执行以下操作:

I have an assignment that tells me to do the following:

创建一个名为 IsPrime() 的方法,它接受一个正整数作为参数.如果是素数,则该方法应返回 true.(质数只能被 1 和它本身整除).将此方法合并到名为 MyMathMethods 的类中.在名为 MainFile 的单独类中创建一个 main() 方法,该方法将通过使用输入对话框向用户询问一个数字来测试 IsPrime(),然后报告该数字是否为素数.

Create a method named IsPrime(), which accepts a positive integer as a parameter. If a number if prime, the method should return true. (A prime number is evenly divisible only by 1 and itself). Incorporate this method into a class named MyMathMethods. Create a main() method in a separate class called MainFile, which will test IsPrime() by asking the user for a number using an input dialog and will then report whether that number is prime or not.

如何连接这两个文件?这是我的代码:

How do I connect these two files? here is my code:

     package x;
     import java.util.Scanner;
    public class MyMathMethod
 { 
 public static boolean isPrime(int num)
 {


 int result=0;
   System.out.println("enter no");
  Scanner s = new Scanner(System.in);
  num =s.nextInt();
  long sqrt = (int) Math.sqrt(num);
  for(long divisor = 2; divisor <= sqrt; divisor++) 
 {
        if(num % divisor == 0)
{  
                // This only needs to happen once
              // for this number to NOT be prime
  return false;
        }
}
// If we get here, the number is prime.
return true;
  }
   }

另一个文件是

   import x.*;
   package y;
 public class MainFile {
  public static void main(String [] args) {

  boolean isNumberPrime;
    int num;
 MyMathMethod methods = new MyMathMethod();
 isNumberPrime = methods.isPrime(num);
{

如果(数字=真)System.out.println(num + "是质数");别的System.out.println(num + " 不是质数");}}}

if(num=true) System.out.println(num + " is Prime Number"); else System.out.println(num + " is not Prime Number"); } } }

提前致谢.

推荐答案

是否要从另一个类中的 main 调用 isPrime() 方法?

Do you want to call the method isPrime() from your main in another class?

当它们在同一个包中时,您必须创建类 MyMathMethods 的新实例并调用方法 isPrime().当它们在不同的类中时,您必须导入缺少的类并执行与上述相同的操作.如果您不手动导入它,您的 IDE 可能会为您完成或要求它.

When they're a in the same package, you have to create a new instance of your class MyMathMethods and call the method isPrime(). when they're in different class, you must import the missing class and do the same as above. If you don't import it manually, probably your IDE will do it for you or ask for it.

第一个文件:

package x;
public class MyMathMethods {
  public boolean isPrime(int number) {
    //your code here
    return false;
  }
 }

第二个:

package y;
import x.* //importing all files from the package x
           //if your class MyMathMethods is in the same package, you don't need that
public class MainFile {
  public static void main(String [] args) {
    int number = 20; 
    boolean isNumberPrime;
    MyMathMethods methods = new MyMathMethods();
    isNumberPrime = methods.isPrime(number); //here the result if it's prime
  }
 }

<小时>

我会尝试修复您的代码.


I'll try to fix your code.

如果你想让你的方法是静态的,你可以调用它:

If you want to have your method static, you can call it:

MyMathMethods.isPrime(numer);

无需创建新实例.

下一个问题:为什么要向函数传递一个未初始化的变量(num)并尝试从该方法的输入中获取该值?嗯,这不是一个好习惯.我宁愿建议从 main 中获取用户的输入(直接在 main 中在 main 中调用的另一个函数中),然后将值传递给 isPrime().

The next issue: why are you passing to the funtion a variable that is not initialized (num) and try to get this value from the input in this method? Well, that's not a good practise. I would rather suggest to get the input from the user in main (directly in main on in another function called up in main) and then pass the value to isPrime().

package y;
import x.* 
public class MainFile {
  public static void main(String [] args) {
    int num;
    Scanner s = new Scanner(System.in);
    num =s.nextInt();
    if(MyMathMethods.isPrime(num)) {
      //if true, your code;
    } else {
      //false, your code;
    }
  }
 }

 package x;
 public class MyMathMethod { 
 public static boolean isPrime(int num) {
   if (num <= 1) {           
     return false;
   }
   for (int i = 2; i < Math.sqrt(num); i++) {
     if (num % i == 0) {
       return false;
     }
   }
   return true; 
 }
}

您的算法对许多数字都失败了,例如0, 1, 2.好吧,对于这个值,它甚至没有返回任何值.请始终保护您的方法免受任何可能的错误参数的影响.

Your algoritm failed for many numbers, e.g. 0, 1, 2. Well, for this values it doesn't even returned any value. Please always protect your method against any possible wrong parameters.

这篇关于java中的素数测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆