【发布时间】:2017-01-15 02:33:35
【问题描述】:
我正在编写一个可以玩游戏的 Java 程序。 基本上你选择没有。玩家和回合,然后程序会根据以下规则显示每个玩家应该说的顺序:
-假设玩家站成一个圆圈,他们开始顺时针一个接一个地计数,直到有人找到一个仅由相同数字组成的数字(大于 10)。例如 11、22、33、..、444 等,然后它们开始逆时针计数
例如:P9:9; P10:10; P11:11; P12:13; P11:14 个等(P10 = 玩家 10)
-当得到一个是 7 的倍数、包含 7 或数字之和为 7 的数字时,他们说“Boltz”
例如:P1:13; P2:Boltz(而不是 14); P3:15; P4 博尔兹 (16); P5:博尔茨(17); P6:18 等
我有 Java 代码,但我似乎无法在仅由一位数字组成的数字上从顺时针转向逆时针
你能帮我解决一下 SameDigits 功能吗?谢谢!
import java.util.Scanner;
public class Boltz {
private static Scanner keyboard;
public static void main(String[] args) {
keyboard = new Scanner(System.in);
int nPlayers = 0;
int nRounds = 0;
int currentPlayer = 0;
int sum = 0;
int x = 0;
boolean isSameDigit = true;
System.out.print("Cati jucatori sunt? ");
nPlayers = keyboard.nextInt();
System.out.print("Cate runde sunt? ");
nRounds = keyboard.nextInt();
System.out.print("Jucatori: " + nPlayers + "; Runde: " + nRounds + "\n");
for (x = 1; x <= nPlayers * nRounds; x++) {
isSameDigit = SameDigits(currentPlayer);
if (currentPlayer < nPlayers && isSameDigit == false) {
currentPlayer++;
} else {
currentPlayer = 1;
}
if (currentPlayer > 1 && isSameDigit == true) {
currentPlayer--;
} else {
currentPlayer = nPlayers;
}
sum = digitSum(x);
if (x % 7 == 0 || String.valueOf(x).contains("7") || sum == 7) {
System.out.println("P:" + currentPlayer + " Boltz");
} else {
System.out.println("P:" + currentPlayer + " " + x);
}
}
}
public static int digitSum(int num) {
int suma = 0;
while (num > 0) {
suma = suma + num % 10;
num = num / 10;
}
return suma;
}
public static boolean SameDigits(int num) {
int add = 0, add2 = 0;
while (num > 0) {
add = add + num % 10;
add2 = add2 + add % 10;
num = num / 10;
}
if (add == add2) {
return true;
} else {
return false;
}
}
}
【问题讨论】: