如何确定数字的位数并确定要运行的循环数?
例如,如果我有一个数组int[] a= {123,342,122,333,9909}
和int max = a.getMax()
,我们得到值9909.我想得到数字位值 9909,这是千 - 第一名。
例如......
(number place value,number of loop to run)
(one,1 time)
(tenth,2 time)
(hundred,3 time)
(thousand,4 time)
(ten thousand,5 time)
(hundred thousand,6 time)
这是我的代码,但是当它在整数...
之间达到零时失败public static int getMax(int[] t,int n){
int maximum = t[0]; // first value of the array
int index = 0;
int div=1;
int numSpace=0;
int valueTester=34;
boolean done=false;
for (int i=1; i<n; i++) {
if (t[i] > maximum) {
maximum = t[i]; // maximum
index = i; // comparing index
}
}
while(done==false){
if (valueTester==0){
done=true;
}
else{
valueTester=(maximum / div) % 10;
div=div*10;
numSpace++;
}
}
return numSpace;
}
}
答案 0 :(得分:10)
您可以使用对数。
double[] values = {4, 77, 234, 4563, 13467, 635789};
for(int i = 0; i < values.length; i++)
{
double tenthPower = Math.floor(Math.log10(values[i]));
double place = Math.pow(10, tenthPower);
System.out.println(place);
}
答案 1 :(得分:1)
以下代码段可用于获取整数中百分之元的值:
public int place(int i) {
int j=(i/100)%10;
return j;
}
答案 2 :(得分:0)
要确定数字的位置,可以将整数转换为字符串,并获得它的长度。
例如......
int[] a= {123,342,122,333,9909};
int maxNumber = a.getMax(); // will return '9909'
int numberPlace = (new Integer(maxNumber)).toString().length; // will return '4'
然后你需要获得该地点的英文价值,例如......
String[] placeNames = new String[]{"zero","ones","tens","hundreds","thousands"};
String placeString = placeNames[numberPlace]; // will return "thousands"
这就是你要问的一切吗?我不确定我是否理解你的其余问题
答案 3 :(得分:-2)
int a = 9909;
switch(a)
{
case a < 10:
//ones place
break;
case a < 100:
//hundreds place
break;
//etc.....
}
希望这有帮助。