运行一个循环,最大数字的位数。

huangapple go评论67阅读模式
英文:

Run a Loop of maximum digits of number

问题

例如,我们有一个数字355,那么它的数字计数为3。
我们需要在Java中编写一个程序,该程序从100运行到999。
如果数字是4,则从1000到9999运行一个循环。
如果是5,则从10000到99999运行循环。

英文:

Eg. We have a number 355 then its digit counts are 3.
We have to write a program in Java which runs a loop from 100 to 999.
If digits are 4 then run a loop from 1000 to 9999.
If 5 then 10000 to 99999.

答案1

得分: 2

如果digits为4,则您的循环边界为103(1000)和104 - 1(9999)。

在Java中,您可以使用Math.pow(double a, double b)来计算ab,从而轻松计算上限和下限:

int min = (int) Math.pow(10, digits - 1);
int max = min * 10 - 1;

然后您只需使用这些值编写一个for循环。

英文:

If digits is 4, then your loop boundaries are 10<sup>3</sup> (1000) and 10<sup>4</sup> - 1 (9999).

In Java, you can use Math.pow(double a, double b) to calculate a<sup>b</sup>, making it easy to calculate the upper and lower boundaries:

int min = (int) Math.pow(10, digits - 1);
int max = min * 10 - 1;

Then you just write a for loop using those values.

答案2

得分: -1

我真的不知道是否理解你的问题...
这个循环将运行与num中的数字位数相同的次数

int num = 1000;
String str = "" + num;
for (int i = 0; i < str.length(); i++) {
    // 操作
}
英文:

I really don't know if understand your question...
This loop will run as many times as there are digits in num

int num = 1000;
String str = &quot;&quot; + num;
for (int i = 0; i &lt; str.length(); i++) {
    // stuff
}

huangapple
  • 本文由 发表于 2020年4月4日 09:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61022734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定