我一直試圖向數字(數字爲零)添加+1(數字爲零),該數字已經在數組中,但它不工作。添加到數組中的int
int i=0;//var for arrays
int [] countArray = new int[10];
/////////////////////
//__________ ask for values --------------
System.out.println("Hello please enter the number you would" +
" like to be sorted separated by commas. \n" +
"Example: \" 2,3,5,83,2 \".\t only use" +
" commas. to separate numbers\n");
//----------- save values -----
Scanner scan = new Scanner(System.in);
String allInput = scan.nextLine();//single string object with all input
String [] arr = allInput.split(",");//string array that holds all values
//as String
int [] numbersArray =new int[arr.length] ;//numbers
for (String w: arr){//change Strings to Int
numbersArray[i]= Integer.valueOf(arr[i]);
i++;
}
// __集數都數到零,因爲必要的
i=0;
for (int x: countArray){//set all numbers to zero
countArray[i]=0;
i++;
} //everything zeroed
i=0;
這工作了,感謝你們:
for (int x = 0; x < numbersArray.length; x++){
if (numbersArray[x] >=10 && numbersArray[x] <=100) {
countArray[(numbersArray[x]-1)/10]++;}
else{
if (numbersArray[x] >=0 && numbersArray[x] <=10)
{
countArray[1 -1]++;}
}
}
幾個sysos和調試都會有幫助。 – TheLostMind
將你的'for'循環中的每一個更改爲一個簡單的'for(int i = 0; i
另外,在最後一個'for'循環中,你可以簡單地計算一些'int index =(numbersArray [i] -1)/ 10',並且在驗證它在0和9之間後,用'countArray [index] + +'。 –