【问题标题】:Detecting when an integer is made up of only the same digit检测整数何时仅由相同的数字组成
【发布时间】: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;
    }

  }

}

【问题讨论】:

    标签: java integer digit


    【解决方案1】:

    如果我对您的理解正确,您希望 SameDigits 返回 true 如果数字都是相同的数字,否则返回 false。一位数的数字也应该返回true。应该这样做:

    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;
    }
    

    附:您应该遵守 Java 命名约定,并以小写字母开头(sameDigits 而不是 SameDigits)命名您的方法。

    【讨论】:

      【解决方案2】:

      应该是这样的:

      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;
      }
      

      【讨论】:

      • 为什么number &lt; 10时返回false?
      • 我基于以下规则“假设玩家站在一个圆圈中,他们开始顺时针一个接一个地计数,直到有人达到一个仅由相同数字组成的数字(大于 10)。例如 11、22、33、..、444 等,然后它们仅开始逆时针计数”。此外,它很容易改变;)
      • 很公平。我猜这不是最好的方法名称。
      • 好主意,但它会更有效:return string.replaceAll(string.substring(0, 1), "").length() == 0; 而不是遍历所有可能的单个数字字符串。
      • for (int i = 1; i &lt; string.length(); i++) if (string.charAt(i) != string.charAt(0)) return false;
      【解决方案3】:

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

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

      【讨论】:

      • 正则表达式不应该是"^(\\d)\\1+$"吗?
      • @shalvah 不,不需要。 matches 方法无论如何都匹配整个字符串 - 绝对不需要添加 ^$
      • 抱歉,我是正则表达式的新手。 “\\1”部分是干什么用的?
      • \1\9 这样的序列引用括号中的匹配项。所以这意味着重复与第一组括号的内容匹配的任何内容 - 在这种情况下,\d
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2016-01-04
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2019-07-06
      • 2012-10-21
      相关资源
      最近更新 更多