Prime测试,2位数字 [英] Prime test, 2-digit numbers

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

问题描述

我想要打印所有长度为2位数的素数。这是我的代码:

I want to print all prime numbers that are 2-digits long. Here is my code:

    for(int input = 11; input <= 99; input += 2){
        for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){
            if(input%x != 0){
                System.out.println(input);
                break;
            }else{
                break;
            }
        }
    }

问题在于打印35或49之类的数字不是素数。

The problem is that it prints numbers like 35 or 49 which are not prime numbers.

推荐答案

这有效。您可以在此处查看输出。

This works. You can see the output here.

public class Main {

public static void main(String[] args) {
for(int input = 11; input <= 99; input += 2){
    boolean found = false;
    for(int x = 2; x < (int)Math.sqrt(input) + 1; x++){
        if(input%x == 0){
            found = true;
            break;
        }

    }
    if(!found) {
            System.out.println(input);
    }
}
}
}

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

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