JAVA 逢七过 随机开始报数

 随机开始报数,每7和7的倍数,说出和说错则游戏失败

package com.zhiyou.Game;

import java.util.Scanner;


public class NotSaySeven {
    public static void main(String[] args) {
        startGame();
    }
    //顺序输入,不能包含'7',或者‘7’的倍数;
    //如果该输入得数字是包含'7',或者‘7’的倍数,
    //只要不输入包含'7',或者‘7’的倍数的数字即可;
    public static void startGame() {
        Scanner input = new Scanner(System.in);
        int score = 0;
        int beginNum = (int) (Math.random() * 10) + 1;
            System.out.println("游戏开始从" + beginNum + "开始输入");
            for (int i = beginNum; i > 0; i++) {
                int temp = input.nextInt();
                if (Game(i)) {
                    if (i == temp) {
                        score++;
                    } else {
                        System.out.println("输入错误");
                        break;
                        }
                } else {
                    if (i == temp) {
                        System.out.println("输入错误");
                        break;
                    }
                    score++;
                }
            }
        System.out.println("得分为" + score);
    }

    //判断是否为7的倍数或者包含7
    //是或者包含返回false否则返回true
    public static boolean Game(int a) {
        if (a % 7 == 0) {
            System.out.println(a + "为7的倍数");
            return false;
        }
        while (a > 0) {
            if (a % 10 == 7) {
                System.out.println(a + "包含7");
                return false;
            } else {
                if (a < 10) {
                    return true;
                }
                a /= 10;
            }
        }
        return true;
    }
}

测试结果

"C:\Program Files\Java\jdk-11.0.15.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\lib\idea_rt.jar=53245:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\bin" -Dfile.encoding=UTF-8 -classpath D:\WorkSpace\Java\out\production\Java;C:\Users\Zhizhengchen\.m2\repository\junit\junit\4.13.1\junit-4.13.1.jar;C:\Users\Zhizhengchen\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.zhiyou.Game.NotSaySeven
游戏开始从10开始输入
10
11
12
13
14
14为7的倍数
输入错误
得分为4

Process finished with exit code 0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值