目录
(素数:一个大于1的自然数,除了1和它本身外,不能被其他自然数整除)
03:随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
04:写一个方法,此方法实现输出100-999之间的水仙花数。
09:打印出1000-2000年中所有的闰年,并以每行四个数的形式输出
10:定义两个整数a、b,a、b的取值范围在0-9之间,给出所有符合a+b=12的组合。
-
-
- /*
- * 练习01:求10以内的偶数的和。
- */
- public static void main(String[] args) {
- int sum = 0;
- int num = 0;
-
- while (num <= 10) {
- if (num % 2 == 0) {
- sum += num;
- }
- num++;
- }
- System.out.println("10以内偶数之和:" + sum);
-
- }
-
-
-
- /*
- * 练习02:求100以内的所有素数 (素数:一个大于1的自然数,除了1和它本身外,不能被其他自然数整除)
- *
- */
- public static void main(String[] args) {
- int i, j;
-
- for (i = 2; i <= 100; i++) {
- for (j = 2; j < i; j++) {
- if (i % j == 0)
- break;
- }
- if (j >= i) {// 也可以写成j==i
- System.out.print(i + " ");
- }
- }
-
- }
-
-
- /*
- * 练习03:随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- Scanner sc = new Scanner(System.in);
-
- // 随机产生一个1-100之间的整数
- int num = (int) (Math.random() * 100 + 1);
- System.out.println(num);
-
- // 设置猜测的次数
- int count = 1;
-
- while (count <= 7) {
- System.out.println("请输入你猜测的数字:");
- int guess = sc.nextInt();
- if (guess > num) {
- System.out.println("大了");
- count++;
- } else if (guess < num) {
- System.out.println("小了");
- count++;
- } else {
- System.out.println("猜对了");
- break;
- }
-
- }
-
- if (count > 7) {
- System.out.println("只能猜测7次,机会用完了");
- } else {
- System.out.println("猜了" + count + "次");
- }
-
- sc.close();
-
- }
-
-
-
- /*
- * 练习04:写一个程序,此程序实现输出100-999之间的水仙花数。
- */
- public static void main(String[] args) {
- int num = 100;
- while (num <= 999) {
- int geWei = num % 10;
- int shiWei = num / 10 % 10;
- int baiWei = num / 100;
- if (geWei * geWei * geWei + shiWei * shiWei * shiWei + baiWei * baiWei * baiWei == num) {
- System.out.println(num);
- }
- num++;
- }
-
- }
-
-
-
- /*
- * 练习05:输出小写的a-z以及大写的Z—A
- * ASCII码:
- * A~Z:
- * a-z:
- */
- public static void main(String[] args) {
- for (int i = 97; i <= 122; i++) {
- char ch = (char) i;
- System.out.print(ch + " ");
- }
-
- System.out.println();
-
- for (int i = 65; i <= 90; i++) {
- char ch = (char) i;
- System.out.print(ch + " ");
- }
-
- }
-
-
-
- /*
- * 练习06:求出1-3+5-7+9-…..101的和
- */
- public static void main(String[] args) {
- // 定义一个变量接收相加的和
- int sum = 0;
- int count = 1;
-
- for (int i = 1; i <= 101; i += 2) {
- //偶位数做减法,奇位数做加法
- if (count % 2 != 0) {
- sum = sum + i;
- } else {
- sum = sum - i;
- }
- count++;
- }
- System.out.println("1-3+5-7+9-…..101=" + sum);
- }
-
-
-
- /*
- * 练习07:求出1-1/2+1/3-1/4…..1/100的和
- */
- public static void main(String[] args) {
- double num = 1; // 分子始终为1,设i为分母
- double sum = 0;
- for (int i = 1; i <= 100; i++) {
- if (i % 2 == 0) {
- sum -= num / i;
- } else {
- sum += num / i;
- }
-
- }
- System.out.println("1-1/2+1/3-1/4…..1/100的和是:" + sum);
- }
-
-
-
- /*
- * 练习08:输出20-80之间能被3整除的整数,每5个一行
- */
- public static void main(String[] args) {
- int num = 20;
- int count = 0;
- while (num <= 80) {
- if (num % 3 == 0) {
- System.out.print(num + " ");
- count++;
-
- if (count % 5 == 0) {
- System.out.println();
- }
- }
-
- num++;
- }
-
- }
-
-
-
- /*
- * 练习09:打印出1000-2000年中所有的闰年,并以每行四个数的形式输出
- */
- public static void main(String[] args) {
- int year = 1000;
- int count = 0;
- while (year <= 2000) {
- if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
- System.out.print(year + " ");
- count++;
-
- if (count % 4 == 0) {
- System.out.println();
- }
- }
-
- year++;
- }
-
- }
-
-
-
- /*
- * 练习10:定义两个整数a、b,a、b的取值范围在0-9之间,给出所有符合a+b=12的组合。
- */
- public static void main(String[] args) {
- for (int a = 0; a <= 9; a++) {
- for (int b = 0; b <= 9; b++) {
- if (a + b == 12) {
- System.out.println(a + "+" + b + "=" + (a + b));
- }
- }
- }
-
- }
-