检测整数何时仅由相同的数字组成

Detecting when an integer is made up of only the same digit

我正在编写一个 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:玻尔兹(而不是 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;
    }

  }

}

那会是这样的:

public static boolean sameDigits(int number) {
        //speical case
        if (number < 10)
            return false;
        String string = String.valueOf(number);

        for (int i = 1; i <= 9; i++) {
            if (string.replaceAll(String.valueOf(i), "").length() == 0)
                return true;
        }
        return false;
}

如果我对你的理解正确,你希望 SameDigits 到 return true 如果数字都是相同的数字,否则 false。 Single-digit号也应该returntrue。应该这样做:

public static boolean SameDigits(int num) {
    if (num < 0) return false; // or something else?

    int onesDigit = num % 10;
    num /= 10;
    while (num > 0) {
        if (onesDigit != num % 10) return false; // fail if digits differ
        num /= 10;
    }
    return true;
}

P.S。您应该遵守 Java 命名约定,并以 lower-case 字母开头(sameDigits 而不是 SameDigits)命名您的方法。

我会使用正则表达式。这个检查 String 是否由一个数字组成,后跟一个或多个重复的相同数字。

public static boolean sameDigits(int arg) {
    return Integer.toString(arg).matches("(\d)\1+");
}