As the title says I just need some help/advice on how I can simplify a part of my code. I get the output I want but it's obvious to see that the way I go about it is a bit excessive. What I'm trying to do in my program is pass the array
int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23};
Run Code Online (Sandbox Code Playgroud)
Into my buildFeetArray method which just takes the array elements, divides them by 12 to get a new element value which is then put in a new array which is returned. Here is the method
public static int[] buildFeetArray(int[] arrayParam) {
int sum = 0;
int lessthan1 = 0;
int lessthan2 = 0;
int lessthan3 = 0;
int lessthan4 = 0;
int lessthan5 = 0;
int lessthan6 = 0;
int lessthan7 = 0;
int lessthan8 = 0;
for (int count = 0; count < arrayParam.length; count++) {
if (arrayParam[count] / 12 == 0) {
lessthan1++;
} else if (arrayParam[count] / 12 == 1) {
lessthan2++;
} else if (arrayParam[count] / 12 == 2) {
lessthan3++;
} else if (arrayParam[count] / 12 == 3) {
lessthan4++;
} else if (arrayParam[count] / 12 == 4) {
lessthan5++;
} else if (arrayParam[count] / 12 == 5) {
lessthan6++;
} else if (arrayParam[count] / 12 == 6) {
lessthan7++;
} else if (arrayParam[count] / 12 == 7) {
lessthan8++;
}
}
int[] newArray = {lessthan1, lessthan2, lessthan3, lessthan4, lessthan5, lessthan6, lessthan7, lessthan8};
return newArray;
}
Run Code Online (Sandbox Code Playgroud)
Ideally the output should be
int length = 8;
[0] = 1;
[1] = 2;
[2] = 1;
[3] = 1;
[4] = 1;
[5] = 2;
[6] = 2;
[7] = 2;
Run Code Online (Sandbox Code Playgroud)
Which it is but there's definitely an easier way to go about it, if possible I'd like to avoid using lists and sticking with loops as I need practice with them. Thank you in advance for any advice/tips.
Sye*_*san 21
我为此编写了一些伪代码,其中您必须初始化一个数组,并在满足特定条件时递增数组的特定索引:
public static int [] buildFeetArray(int [] arrayParam) {
int index;
int [] lessthan = {0,0,0,0,0,0,0,0};
for (int count = 0; count < arrayParam.length; count++) {
index = arrayParam[count]/12;
if(index < 8 ) {
lessthan[index]++;
}
}
return lessthan;
}
Run Code Online (Sandbox Code Playgroud)
Arn*_*aud 11
您可能要使用另一个数组来存储结果,例如:
public static int[] buildFeetArray(int [] arrayParam) {
int[] lessThanArray = new int[8];
for (int count = 0; count < arrayParam.length; count++) {
for (int remainder = 0; remainder < lessThanArray.length; remainder++) {
if (arrayParam[count] / 12 == remainder) {
lessThanArray[remainder]++;
break; // exit from the inner "for" loop
}
}
}
return lessThanArray;
}
Run Code Online (Sandbox Code Playgroud)
MWB*_*MWB 10
那这个呢:
int[] lessThanArray = new int[8];
for (int entry: myInches) {
int lessThan = entry / 12;
if (lessThan < 8) {
lessThanArray[lessThan]++;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
You could use a Switch block:
switch(arrayParam[count]/12){
case 0:
lessthan1++;
break;
case 1:
lessthan2++;
break;
//and so on...
}
Run Code Online (Sandbox Code Playgroud)
The effect is the same but it looks way more clean and it comes in handy in situations such like this one