【问题标题】:prime numbers test in javajava中的素数测试
【发布时间】:2015-09-06 14:00:15
【问题描述】:

我有一个任务告诉我要执行以下操作:

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

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

     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 + " 不是质数"); } } }

提前致谢。

【问题讨论】:

  • 没有任何尝试..??
  • 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。

标签: java primes


【解决方案1】:

您想在另一个类中从您的 main 调用方法 isPrime() 吗?

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

第一个文件:

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
  }
 }

编辑:我会尝试修复您的代码。

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

MyMathMethods.isPrime(numer);

无需创建新实例。

下一个问题:为什么要向函数传递一个未初始化的变量(num)并尝试从该方法的输入中获取该值?好吧,这不是一个好习惯。我宁愿建议在 main 中获取用户的输入(直接在 main 中调用 main 中的另一个函数),然后将值传递给 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。好吧,对于这个值,它甚至没有返回任何值。请始终保护您的方法免受任何可能的错误参数的影响。

【讨论】:

  • 请发布您的代码。也许您的类或方法是私有的、受保护的或带有默认修饰符?在这种情况下,它们不会在任何其他包中可见。
  • 我把文件放在我的问题中请回复我
  • 它可以编译吗?我不这么认为。给出什么错误?顺便说一句,这是什么意思?整数;如果(数字=真){} ?????首先,如果要比较它,请使用==。其次,在 java 中比较 int 和 boolean 从来都不是一个好主意。我想它甚至不会编译。
  • 我知道它不能编译这就是问题你有更好的代码吗?请帮帮我
  • 再次编辑。如果现在有效,请接受答案。
猜你喜欢
  • 1970-01-01
  • 2013-06-09
  • 2017-03-05
  • 1970-01-01
  • 2013-10-31
相关资源
最近更新 更多