例如:
int getMax(int a) {
return max;
}
a : 1 --> max : 9,
a : 2 --> max : 99,
a : 3 --> max : 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;
}
}
或者您可以使用 Math.pow()
,因为每个结果都是 10a - 1:
public static int getMax(int a) {
// TODO: Validate argument
return (int) (Math.pow(10, a) - 1);
}
关于java - 如何在java中获得小数点位置的最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28272711/