⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 primes.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 JAVA
字号:
//Chapter 3, Exercise 2

public class Primes {
  public static void main(String args[]) {
    int maxValue = 50;         // The maximum value to be checked.

    // Check all values from 2 to maxValue:
    OuterLoop:
    for(int i=2 ; i<=maxValue ; i++) {
      // Try dividing by all integers from 2 to square root of i:
      for(int j=2 ; j<=Math.sqrt(i) ; j++) {
        if(i%j == 0)                      // This is true if j divides exactly
          continue OuterLoop;             // so exit the loop.
      }
      // We only get here if we have a prime:
      System.out.println(i);   // so output the value.
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -