歌德巴赫猜想_Java
程序员文章站
2024-01-31 20:39:16
...
import java.util.*; public class Welcome { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("输入一个大于6的偶数: "); int num = in.nextInt(); if (num > 6 && (num % 2 == 0)) { Guess g = new Guess(num); g.getValue(); } else { System.out.println("不符合输入规范..."); } in.close(); } } class Guess { private int num; public Guess () { this(8); } public Guess (int num) { this.num = num; } private boolean isPrime(int i) { for (int j = 2; j < i - 1; j++) { if (i % j == 0) { return false; } } return true; } public void getValue() { for (int i = 4; i <= this.num; i = i + 2) { for (int a = 2; a <= i / 2; a++) { if (isPrime(a) && isPrime(i - a)) { System.out.println((i + " = " + a + "+" + (i - a))); break; } } } } }
上一篇: JS代码整洁之道个人感悟
下一篇: 通过CORS配置实现JS跨域访问