1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| import java.util.*; public class JBT2501 { public static void main(String[] args) { myMath mymath = new myMath(); int choice = 1; int i = 0, m = 0, n = 0; long sum = 0, fact = 0, p = 0; Scanner stdin = new Scanner(System.in); while (choice != 0) { System.out.println("\n\n"); System.out.println("主要功能"); System.out.println("========================"); System.out.println("1.输出1+2+...+n的结果"); System.out.println("2.输出1*2*...*n的结果"); System.out.println("3.输出两个整数的最大值"); System.out.println("4.求使得1+2+...+n的值大于p的最小n值"); System.out.println("5.求m和n的之间的素数"); System.out.println("6.退出系统"); System.out.print("选择[1..6]:"); choice = stdin.nextInt(); System.out.println(""); switch (choice) { case 1: System.out.print("请输入n:"); n = stdin.nextInt(); System.out.println(""); System.out.println("sum(1.." + n + ")=" + mymath.sum(n)); break;
case 2: System.out.print("请输入n:"); n = stdin.nextInt(); System.out.println(""); System.out.println(n + "!=" + mymath.fact(n)); break;
case 3: System.out.print("请输入m:"); m = stdin.nextInt(); System.out.println(""); System.out.print("请输入n:"); n = stdin.nextInt(); System.out.println(""); System.out.println("max(" + m + "," + n + ")=" + mymath.max(m, n)); break;
case 4: System.out.print("请输入p:"); p = stdin.nextInt(); System.out.println(""); System.out.println("(1+2+..+n)>=" + p + "的最小n=" + mymath.minN(p)); break;
case 5: System.out.print("请输入m:"); m = stdin.nextInt(); System.out.println(""); System.out.print("请输入n:"); n = stdin.nextInt(); System.out.println(""); mymath.dispPrimeNumber(m, n); break;
case 6: System.out.println("退出系统......"); return; } } } }
class myMath { long sum(int n) { long sum1 = 0; int i; for (i = 1; i <= n; i++) { sum1 = sum1 + i; } return sum1; }
long fact(int n) { long fact1 = 1; int i; for (i = 1; i <= n; i++) fact1 = fact1 * i; return fact1; }
int max(int x, int y) { return x > y ? x : y; }
int minN(long m) { long sum = 0; int n = 0; while (sum < m) { sum = sum + n; n = n + 1; } return n; }
void dispPrimeNumber(int num1, int num2) { int m, i, k, num = 0; boolean isprime = true; System.out.printf("\n"); for (m = num1; m <= num2; m++) { k = (int) Math.sqrt(m + 1); for (i = 2; i <= k; i++) if (m % i == 0) { isprime = false; break; } if (isprime) { System.out.printf("%-4d", m); num++; if (num % 10 == 0) System.out.printf("\n"); } isprime = true; } System.out.printf("\n[%d:%d]之间素数总共%d\n", num1, num2, num); } }
|