循环练习题

一些针对前一篇文章的算法题.
文末外加补充了 Random 类.

(一) 折纸

世界最高山峰是珠穆朗玛峰 (8844.43米=8844430毫米), 假如我有一张足够大的纸,它的厚度是0.1毫米.

请问, 我折叠多少次, 可以折成珠穆朗玛峰的高度?

public class homework9 {
    public static void main(String[] args) {
        float a = 0.1F;//纸的厚度
        int b = 0;//折叠的次数
        while (a < 8844430) {
            a = a * 2;
            b++;
        }
        System.out.println(b);
    }
}

输出: 27

(二) 回文数

输入一个整数, 如果 x 是一个回文数, 则打印 true; 若不是回文数, 返回 false.

回文数: 正序读和倒序读都是同一个数字, 如121, 12321...

public class homework5 {
    public static void main(String[] args) {
        int x = 12321;
        int num = 0;//记录数字颠倒后的一个新的数字.
        int temp = x;//储存 x 的值
        while (x != 0){
            int ge = x % 10;//取 x 的个位.
            x = x / 10;//x 去掉个位.
            num = num * 10 + ge;//把获取的个位放在新数的后面
        }
        System.out.println(temp == num);//如果 x = 新的数, 那么他是回文数.
    }
}

输出: true

(三) 除法但是不能用除

给两个整数 (都是正数, 且不超过 int 的取值范围), 将两数相除, 得到商和余数.

要求: 不使用 *, /, %.

public class homework8 {
    public static void main(String[] args) {
        //Scanner scanner = new Scanner(System.in);
        int divident = 15;//被除数.
        int divisor = 10;//除数.
        int count = 0;//除了多少次 = 商
        int temp = divident;
        for (; divident - divisor > 0; count++) {
            divident = divident - divisor;
        }
        System.out.println(temp  + "/" + divisor + "=" + count + "..." + divident);
    }
}

输出: 15/10=1...5

(四)逢 7 过

朋友聚会的时候可能会玩一个游戏: 逢 7 过.

游戏规则: 从任意一个数字开始报数, 当你要报的数字是包含7或者是7的倍数时都要说过: "过".

需求: 使用程序在控制台打印出 1-100 之间的满足逢七必过规则的数据.

//数字包含 7 , 即 (i - 7) % 10 == 0 || (i / 10 - 7) % 10 == 0
//(减去 7 的取余为 0, 则包含 7, 个位十位都要处理)
//可以简化为 i % 10 == 7 || (i / 10) % 10 == 7

public class homework10 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if (i % 7 == 0 || i % 10 == 7 || (i / 10) % 10 == 7) {
                System.out.print(i + ", ");
            }
        }
    }
}

输出:

7, 14, 17, 21, 27, 28, 35, 37, 42, 47, 49, 56, 57, 63, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 84, 87, 91, 97, 98, 

(五) 平方根

键盘录入一个大于等于 2 的整数 x, 计算并返回 x 的平方根.

结果只保留整数部分, 小数部分舍去.

//当 i * i = x 时, i 为 x 的平方根.
//当 i * i < x 且有 j * j > x, 那么 x 的平方根就在 i~j 之间,
//平方根的整数部分就是刚好大于 x 的那个 j 减 1.

import java.util.Scanner;

public class homework10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        for (int i = 1; i <= x; i++) {
            if (i * i == x) {
                System.out.println("平方根为 " + i);
                break;
            } else if (i * i > x) {
                System.out.println("平方根的整数部分为 " + (i - 1));
                break;
            }
        }
    }
}

(六) 判断质数

需求: 键盘录入一个正整数X, 判断该整数是否为一个质数.

质数: 只能被 1 和它本身整除.

错误示范:

import java.util.Scanner;

public class homework10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        for (int i = 2; i < x; i++) {
            if (x % i == 0){
                System.out.println("不是质数.");
                break;
            }
//            else{
//                System.out.println("是质数");
//                break;
//            }
            
//不可以用else.
//这段else的意思是: 如果这个数没被 i 整除, 那么它就是质数.
//但是有些数前几个不能整除, 但是后面有能够被非自身的数整除的, 例如 9,
//9 % 2 不等于 0, 不满足 if 就会进入 else, 直接宣判为质数而不去继续算后面能不能被整除.
        }
    }
}

输入: 9

输出: 是质数.

这对吗?

标记思想: 默认值为是, 触发条件再变为否.

import java.util.Scanner;

public class homework10 {
    public static void main(String[] args) {
        boolean flag = true;//先默认一个boolean值为 true.
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        for (int i = 2; i < x; i++) {
            if (x % i == 0) {
                flag = false;//满足条件时, flag 变成false.
                break;
            }
        }
        if (flag){//
            System.out.println("是质数.");
        }else{
            System.out.println("不是质数.");
        }
    }
}

输入: 9

输出: 不是质数.

这下对了.

(七) 猜数字

需求: 程序自动生成一个1-100之间的随机数字,使用程序实现猜出这个数字是多少?

需要用 Random 类生成随机数, 可以右边目录快捷跳转.

import java.util.Random;
import java.util.Scanner;

public class homework10 {
    public static void main(String[] args) {
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        int num = r.nextInt(100)  + 1;
        System.out.println("请输入一个 1 ~ 100 的整数呢~~");
        while (true) {//不知道循环次数, 所以用 while.
            int input = sc.nextInt();//接收 input 需要放在循环内.
            if (input < num) {
                System.out.println("猜小了--杂~鱼~");
            } else if (input > num) {
                System.out.println("猜大了--杂~鱼~");
            } else {
                System.out.println("哇--猜中了呢--好~厉~害~呢~");
                break;//猜中时跳出循环.
            }
        }
    }
}

额外需求: 要求连续猜错 10 次时, 直接给出答案.

import java.util.Random;
import java.util.Scanner;

public class homework10 {
    public static void main(String[] args) {
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        int num = r.nextInt(100)  + 1;
        int count = 0;//添加一个计数器, 用来记录猜的次数.
        System.out.println("请输入一个 1 ~ 100 的整数呢~~");
        while (true) {
            int input = sc.nextInt();
            if (input < num) {
                System.out.println("猜小了--杂~鱼~");
            } else if (input > num) {
                System.out.println("猜大了--杂~鱼~");
            } else {
                System.out.println("哇--猜中了呢--好~厉~害~呢~");
                break;
            }
            count++;
            if (count == 10){//当计数器 = 10, 也就是正好猜错 10 次的时候, 直接触发保底
                System.out.println("答案是 " + num + " 捏~\n这都猜不中, 果真是杂鱼呢~~噗噗噗.");
                break;
            }
        }
    }
}

补充: Random 类

语法:

import java.util.Random;//导包
Random r = new Random();//创建对象
int x = r.nextInt();//生成随机数

括号里是随机范围. 输入一个数, 它会自动出现 bound: , 表示随机数的范围不超过这个数.

如图, 括号里输入 100, 此时随机数的区间为 0 ~ 99 (不包括 100).

屏幕截图 2026-04-12 231810

可以在 int 后面加或者减某个值,使它能够从任意数到任意数随机.

int x = r.nextInt( bound: 5) + 5;//5 ~ 9 随机.
int x = r.nextInt( bound: 5) - 5;//-5 ~ -1 随机

简而言之:

最小值是: 左边加/减的数;

最大值是: 括号里的数 + 左边加/减的数 - 1.

注意: 生成随机数 int x = r.nextInt();不要写在循环里面, 否则每一次循环都会重新生成一个随机数.

posted @ 2026-04-13 00:50  令狐澄禮  阅读(13)  评论(0)    收藏  举报