Java之习题小练—循环控制

循环控制语句的习题小练

习题一 CommandBySwitch命令解析程序

/*
任务要求:
    CommandBySwitch命令解析程序:
    要求:接收用户输入的命令,依据命令做不同的操作
 */

public class CommandBySwitch {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数字1~4:");
        int command = scanner.nextInt();
        commandBySwitch(command);
    }

    public static void commandBySwitch(int command) {
        switch (command) {
            case 1: System.out.println("执行输出 1");
                    break;
            case 2: System.out.println("执行输出 2");
                    break;
            case 3: System.out.println("执行输出 3");
                    break;
            case 4: System.out.println("执行输出 4");
                    break;
            default: System.out.println("Error");
                    break;
        }
    }
}

习题二 for循环:输出5次"行动是成功的阶梯"、输出9的乘法表(1到9、1/3/5/7/9、9到1)、1到100的累加和

/*
任务要求:
    for循环:输出5次"行动是成功的阶梯"、
    输出9的乘法表(1到9、1/3/5/7/9、9到1)、
    1到100的累加和
 */
public class ForLoop {

    public static void main(String[] args) {
        // 输出5次"行动是成功的阶梯"
        System.out.println("输出5次\"行动是成功的阶梯\"");
        for (int i = 0; i < 5; i++) {
            System.out.println("行动是成功的阶梯");
        }
        System.out.println();

        // 输出9的乘法表(1到9)
        System.out.println("输出9的乘法表(1到9)");
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                System.out.printf("%d×%d=%d\t", i, j, i*j);
            }
            System.out.println();
        }
        System.out.println();

        // 输出9的乘法表(1/3/5/7/9)
        System.out.println("输出9的乘法表(1/3/5/7/9)");
        for (int i = 1; i <= 9; i += 2) {
            for (int j = 1; j <= 9; j++) {
                System.out.printf("%d×%d=%d\t", i, j, i*j);
            }
            System.out.println();
        }
        System.out.println();

        // 输出9的乘法表(9到1)
        System.out.println("输出9的乘法表(9到1)");
        for (int i = 9; i >= 1; i--) {
            for (int j = 1; j <= 9; j++) {
                System.out.printf("%d×%d=%d\t", i, j, i*j);
            }
            System.out.println();
        }
        System.out.println();

        // 1到100的累加和
        System.out.println("输出1到100的累加和");
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}

习题三 Guessing猜数字之do…while版

/*
任务要求
    Guessing猜数字之do...while版
    要求:随机生成一个数,由用户来猜,猜不对则反复猜,并给出大小提示,猜对的则结束,用do...while来实现。
 */
public class GuessingDoWhile {

    public static void main(String[] args) {
        Random random = new Random();
        int number = random.nextInt(100) + 1;
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.print("请输入你猜测的数字(1-100):");
            int guess = scanner.nextInt();
            if (guess > number) {
                System.out.println("猜大了");
            } else if (guess < number) {
                System.out.println("猜小了");
            } else {
                System.out.println("恭喜你,猜对了!");
                break;
            }
        } while (true);
    }
}

习题四 Guessing猜数字之while版

/*
任务要求:
    Guessing猜数字之while版:
    要求:随机生成一个数,由用户来猜,猜不对则反复猜,并给出大小提示,猜对的则结束,用while来实现。
 */
public class GuessingWhile {

    public static void main(String[] args) {
        Random random = new Random();
        int number = (int)(1 + Math.random() * 100 - 1 + 1);
        System.out.println(number);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入你猜测的数字(1-100):");
            int guess = scanner.nextInt();
            if (guess > number) {
                System.out.println("猜大了");
            } else if (guess < number) {
                System.out.println("猜小了");
            } else {
                System.out.println("恭喜你,猜对了!");
                break;
            }
        }
    }
}

习题五 Addition随机加法运算器

/*
任务要求:
    Addition随机加法运算器,
    要求:由系统随机出10道加法题,而后由用户来答题,答题后输出"答对
 */
public class AdditionCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int score = 0;
        for (int i = 0; i < 10; i++) {
            // 随机生成两个数字,在[1,100]之间
            int num1 = (int)(1 + Math.random()*(100 - 1 + 1));
            int num2 = (int)(1 + Math.random()*(100 - 1 + 1));

            // 让用户输入答案
            System.out.println("第" + (i+1) + "题");
            System.out.print(num1 + " + " + num2 + " = ");
            int answer = scanner.nextInt();

            // 判断答案是否正确,并更新分数
            if (answer == num1 + num2) {
                System.out.println("答对了");
                score += 10;
            } else {
                System.out.println("答错了");
            }
        }

        System.out.println("总分数:" + score);
    }
}

习题六 MultiTable九九乘法表

/*
任务要求:
    MultiTable九九乘法表,要求:输出九九乘法表
 */
public class MultiTable {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " x " + i + " = " + (i*j) + "\t");
            }
            System.out.println();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值