如何在java中生成小数点位置的最大值

如何在java中生成小数点位置的最大值,java,Java,例如: int getMax(int a) { return max; } a:1-->最大值:9,a:2-->最大值:99,a:3-->最大值: 999 等等。 谢谢。有多种选择。由于您的方法只能返回int,因此没有太多可用选项,因此您可以编写: private static final int[] results = { 9, 99, 999, 9999, ... }; public static int getMax(int a) { // TODO: Validat

例如:

int getMax(int a) {
    return max;
}
a:1-->最大值:9,
a:2-->最大值:99,
a:3-->最大值: 999


等等。


谢谢。

有多种选择。由于您的方法只能返回
int
,因此没有太多可用选项,因此您可以编写:

private static final int[] results = { 9, 99, 999, 9999, ... };

public static int getMax(int a) {
    // TODO: Validate argument
    return results[a - 1];
}
或者你可以循环:

public static int getMax(int a) {
    // TODO: Validate argument
    int result = 9;
    for (int i = 1; i < a; i++) {
        result = result * 10 + 9;
    }
}
他是你的朋友。。
public static int getMax(int a) {
    // TODO: Validate argument
    return (int) (Math.pow(10, a) - 1);
}