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 FilesJavajdk-11.0.15.1injava.exe" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2021.3libidea_rt.jar=53245:C:Program FilesJetBrainsIntelliJ IDEA 2021.3in" -Dfile.encoding=UTF-8 -classpath D:WorkSpaceJavaoutproductionJava;C:Usershizhengchen.m2 epositoryjunitjunit4.13.1junit-4.13.1.jar;C:Usershizhengchen.m2 epositoryorghamcresthamcrest-core1.3hamcrest-core-1.3.jar com.zhiyou.Game.NotSaySeven 游戏开始从10开始输入 10 11 12 13 14 14为7的倍数 输入错误 得分为4 Process finished with exit code 0
