简体   繁体   English

判断一个数是否为素数

[英]Finding if a number is prime

I just started a java course.我刚刚开始了一个java课程。 The task is to write a code that will output whether a number is prime or not.任务是编写一个代码来输出一个数是否为素数。

This is what I have.这就是我所拥有的。 It comes up with a lot of errors.它出现了很多错误。 I have found a lot of answers but they all say to use lines that we haven't learnt.我找到了很多答案,但他们都说要使用我们没有学过的台词。

public class PrimeNumbers2 {
public static void main(String[] args) {
    int number = 9;
    int count = 2;
    Boolean prime = true;

    if (number % count == 0) {
        prime = true;
    }
    else {

        for (count = count + 1) {

        if (count + 1>= number) {
            prime = false;
        }
        else
        {
            prime =  false;
        }

    }
    if (prime == true)
    {
        System.out.println("Number is prime");
    }
     else
    {
        System.out.println("number is not prime");
    }
}}}

You should think about how you would formulate your problem in natural language.您应该考虑如何用自然语言表述您的问题。 For example: A prime number is a number that is only dividable by itself and 1. or the other way around: For every number in the range of bigger then 1 and smaller then my number, there should be no divider .例如:素数是一个只能被它自己和 1 整除的数字。或者反过来对于大于 1 和小于我的数字范围内的每个数字,不应该有除法器

To formulate your problem now in simple java code, try to implement your statement.要现在用简单的 Java 代码来表述您的问题,请尝试实现您的语句。

   for(int count=2; count < number; count++) {
      if(number % count == 0) {
          System.out.println("Number is not a prime!");
          // Since we are finished return and do no more iterations
          return;
      }
   }
   // we iterated all possible numbers and can return successfully
   System.out.println("Number is a prime");

Make also sure to check the edge cases in advance.还要确保提前检查边缘情况。 Eg negative numbers.例如负数。 Please note that is an inefficient solution but the straight forward one.请注意,这是一种低效的解决方案,但很直接。

You have multiple bugs and logic errors in your code.您的代码中有多个错误和逻辑错误。 Here it is cleaned up.这里已经清理干净了。

int number = 15;
int count = 2;
boolean prime = true;


for (int i = count; i <= number/2; i++ ) {

   if (number % i == 0) {
      prime = false;
  }
}


if (prime == true)
{
    System.out.println("Number is prime");
}
else
{
    System.out.println("number is not prime");
}

If we're talking about a super simple implementation here, you could first check to see if the number is even using the remainder operator, ie, if number % 2 == 0 , then prime = true .如果我们在这里谈论一个超级简单的实现,您可以首先检查数字是否使用余数运算符,即,如果number % 2 == 0 ,则prime = true If that is not the case, set count = 3 and set up a while loop with the terminating condition while (count < number) .如果不是这种情况,请设置count = 3并使用终止条件while (count < number)设置while循环。 Within the while loop, check to see if the remainder of number and count is zero.while循环中,检查numbercount的余count是否为零。 If it is, it's a prime number.如果是,它是一个质数。 If not, increment count by one and the loop continues.如果不是,则将countcount继续循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM