用于检查素数的循环

时间:2016-10-06 07:10:11

标签: java if-statement for-loop

package pureTest;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class test3 {    
        public static void main(String[] args) {
            /* Enter your code here. */
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for (int i = 2; i< n; i++){
              if( n <= 3){
                  System.out.println("Prime");
                  break;
              }else if( n%i ==0){
                   System.out.println("Not Prime");
                   break;
              }else{
                  System.out.println("Prime");
              }
            }

          }
      }
输入7;输出是重复的Prime:

7
Prime
Prime
Prime
Prime
Prime

只是想知道为什么if条件在这里不起作用。

4 个答案:

答案 0 :(得分:1)

你的其他条款错了。每次n不能被i整除时,它会打印“Prime”。它甚至会为非素数输入打印prime(例如,它会在打印“Not Prime”之前打印21的“Prime”,因为21%2!= 0)。

将循环更改为:

      for (int i = 2; i < n; i++) {
          if( n <= 3){
              System.out.println("Prime");
              return;
          } else if(n%i == 0){
              System.out.println("Not Prime");
              return;
          }
      }
      System.out.println("Prime");

答案 1 :(得分:1)

你的代码会打印出素数,直到找到除数!

for (int i = 2; i< n; i++){
          if( n <= 3){
              System.out.println("Prime");
              break;
          }else if( n%i ==0){
               System.out.println("Not Prime");
               break;
          }else{
              System.out.println("Prime"); --> this line will be printed every time in your loop!
          }
        }

此外,您不需要迭代到n,因为在n / 2之后将没有可以划分n的数字: - )

检查此代码......

private static boolean checkPrime(int n) {
    int i = 2;
    while(i<=n/2){
        if(n%i++ == 0){
            return false;
        }
    }
    return true;
}

答案 2 :(得分:0)

public static void main(String[] args) {
        /* Enter your code here. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        boolean isPrime = true;
        for (int i = 2; i< n; i++){
          if( n <= 3){
              break;
          }else if( n%i ==0){
               System.out.println("Not Prime");
               break;
          }
        }
        if (isPrime) {
            System.out.println("Prime");
        } else {
            System.out.println("Not prime");
        }
      }

如果您只想查看main功能,则上述操作正常。如果您想这样做,请尝试以下方法:

public static void main() {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    if (isPrime(n)) {
        System.out.println("Prime");
    } else {
        System.out.println("Not prime");
    }
}

boolean isPrime(int n) {
    if (n < 2) {
        return false;
    if (n == 2)
        return true;
    if (n%2 == 0)
        return false;
    for (int i = 3; i*i<=n; i+=2){
        if (n%i == 0)
            return false
    return true;
}

答案 3 :(得分:0)

-(void)selectitem :(UIButton *)sender{

//Get Index path from Button Tag
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];

// get Cell instance and than do whatever you want with Cell items
CustomCell *cell = *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

if ([cell.Selectitems isSelected]) {
    [cell.Selectitems setSelected: NO];
    cell.check = YES;
     [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-uncheck.png"] forState:UIControlStateNormal];
}else{
        [cell.Selectitems setSelected: YES];
        cell.check = YES;
      [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-check.png"] forState:UIControlStateNormal];
}
}