Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 素数:这个代码是如何工作的?_Java - Fatal编程技术网

Java 素数:这个代码是如何工作的?

Java 素数:这个代码是如何工作的?,java,Java,这段代码计算素数,但它是如何工作的? 我需要一个明确的解释,如果(乐队)在结束时正在做什么 public class Primes { public static void main(String[] args) { int m; boolean band; for (int i = 2; i < 100; i++) { m = 2; band = true;

这段代码计算素数,但它是如何工作的? 我需要一个明确的解释,如果(乐队)在结束时正在做什么

public class Primes {

    public static void main(String[] args) {
        int m;
        boolean band;

        for (int i = 2; i < 100; i++) {
            m = 2;
            band = true;

            while (band && m <i) {    
                if (i % m == 0) {
                    band = false;
                } else {
                    m++;
                }
            }

            if (band) {
                System.out.println("The number " + i + " is prime");
            }
        }
    }
}
公共类素数{
公共静态void main(字符串[]args){
int m;
布尔带;
对于(int i=2;i<100;i++){
m=2;
波段=真;

而(band&&m这只是打印2-99之间的素数


从for循环中,它迭代2-99之间的数字。在for循环中,它检查特定的数字(i)可以被m整除,没有任何剩余。如果有剩余,则将m的值增加1,直到m这只是打印介于2-99之间的素数


从for循环中,它迭代2-99之间的数字。在for循环中,它检查特定的数字(i)可以被m整除,没有任何剩余。如果有剩余,则将m的值增加1,直到m尝试重命名变量ble
band
to
i_is_prime
,然后尝试跟踪逻辑。关键行是这一行:

if (i%m==0) {

如果
m
平均分为
i
,则该行返回
true
,这意味着
i
不是素数。

尝试将变量
band
重命名为
i\u is\u prime
,然后尝试跟踪逻辑。关键行是:

if (i%m==0) {

如果
m
平均除以
i
,则该行返回
true
,这意味着
i
不是素数。

此方法用于查找所有小于100的素数

// This is the dividend
int m;    
// Whether can't divisible
boolean band;    

//  From 100 all digital lookup
for (int i=2;i<100;i++){ 
        // Divisor increasing from 2
    m=2;
    // If not divisible, here defined as cannot be divided exactly by first, if we can find a divisor aliquot, the current number is not a prime 
    band=true;
    // From 2 to the current number one by one
    while(band && m <i){    
            // Remainder of 0, explain aliquot, band = false, jump out the while
        if(i%m==0){
            band=false;
        }else{
        // Remainder is not 0, i cannot be divided exactly by m, continue to verify the m + 1
            m++;
        }
    }
    // From 2 to i-1 cannot be divisible, that i was a prime number
    if(band){
        System.out.println("The number "+i+" is prime");
    }
}
//这是红利
int m;
//是否不能分割
布尔带;
//从100全数字查找

对于(inti=2;i这个方法是找到所有小于100的素数

// This is the dividend
int m;    
// Whether can't divisible
boolean band;    

//  From 100 all digital lookup
for (int i=2;i<100;i++){ 
        // Divisor increasing from 2
    m=2;
    // If not divisible, here defined as cannot be divided exactly by first, if we can find a divisor aliquot, the current number is not a prime 
    band=true;
    // From 2 to the current number one by one
    while(band && m <i){    
            // Remainder of 0, explain aliquot, band = false, jump out the while
        if(i%m==0){
            band=false;
        }else{
        // Remainder is not 0, i cannot be divided exactly by m, continue to verify the m + 1
            m++;
        }
    }
    // From 2 to i-1 cannot be divisible, that i was a prime number
    if(band){
        System.out.println("The number "+i+" is prime");
    }
}
//这是红利
int m;
//是否不能分割
布尔带;
//从100全数字查找
对于(int i=2;i
  • 出来后,当“if(band)”正在检查计数器“i”是否正确时 无论是不是最好的
  • 您应该为标识符使用有意义的名称,所以将BAND更改为 是素数
  • 您使用的算法在尝试实现时非常耗时 Eratosthenes的筛,它有O(nlog(log(n)))计算值 复杂性是伟大而简单的

  • 出来后,当“if(band)”正在检查计数器“i”是否正确时 无论是不是最好的
  • 您应该为标识符使用有意义的名称,所以将BAND更改为 是素数
  • 您使用的算法在尝试实现时非常耗时 Eratosthenes的筛,它有O(nlog(log(n)))计算值 复杂性是伟大而简单的

在上面的程序中,什么是不清楚的?我不理解逻辑这只是打印2-99之间的素数。什么是不清楚的?这个问题是如何得到+2的?如果你使用Eclipse这样的好编辑器,你有没有尝试过调试?现在是开始使用IDE的好时机。能够调试是programmin的核心部分g、 在上面的程序中,什么是不清楚的?我不懂逻辑这只是打印2-99之间的素数。什么是不清楚的?这个问题怎么会变成+2?如果你使用Eclipse这样的好编辑器,你试过调试吗?现在是开始使用IDE的好时机。能够调试是编程的核心部分。Thanks!@Ryan Bemrose谢谢!@Ryan Bemrose