This question already has an answer here:
这个问题已经有了答案:
- What's the simplest way to print a Java array? 27 answers
- 打印Java数组最简单的方法是什么?27日答案
The following code is supposed to sort the array initialized below.
下面的代码应该对下面初始化的数组进行排序。
int[] intArray = { 32, 42, 1, 23, 56, 75, 32, 23 };
int temp = 0;
for (int i = 0; i < intArray.length; i++) {
for (int j = 1; j < intArray.length; j++) {
if (intArray[j - 1] > intArray[j]) {
temp = intArray[j - 1];
intArray[j - 1] = intArray[j];
intArray[j] = temp;
}
}
}
What should you type to get the sorted array and where should you type it?
你应该键入什么来获得排序的数组,在哪里输入?
I have tried some options such as System.out.println(temp)
between the last two closing brackets and the one before those, but I am not getting all the values printed, and 32
is printing many times instead.
我尝试了一些选项,比如System.out.println(temp),在最后两个闭括号和之前的闭括号之间,但是我并没有打印所有的值,而是32打印了很多次。
Writing the code System.out.println(j)
or System.out.println(i)
in the same area doesn't work either. Can you explain why these codes don't work?
在同一个区域中编写System.out.println(j)或System.out.println(i)也不起作用。你能解释一下为什么这些代码不起作用吗?
2 个解决方案
#1
0
If all you want to do is print the sorted array, simply print the elements in a for loop after the array is sorted:
如果你想要做的只是打印已排序的数组,那么只需在数组被排序后在for循环中打印元素:
for(int i=0; i < intArray.length; i++)
{
System.out.println(intArray[i]);
}
#2
0
As your code sorts an array, if you print something inside any bracket it will show the process, not the answer. try to print the array after the last bracket.
当你的代码对数组进行排序时,如果你在任何括号内打印东西,它将显示这个过程,而不是答案。尝试在最后一个括号之后打印数组。
int[] intArray={32,42,1,23,56,75,32,23};
int temp = 0;
for(int i=0; i < intArray.length; i++){
for(int j=1; j < intArray.length; j++){
if(intArray[j-1] > intArray[j]){
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
for( int i=0; i<intArray.length; i++ ){
System.out.println(intArray[i]);
}
I'm not a native english speaker so I apologize if there's any grammatical error in my words.
我不是英语母语的人,所以我道歉,如果我的话有语法错误。
#1
0
If all you want to do is print the sorted array, simply print the elements in a for loop after the array is sorted:
如果你想要做的只是打印已排序的数组,那么只需在数组被排序后在for循环中打印元素:
for(int i=0; i < intArray.length; i++)
{
System.out.println(intArray[i]);
}
#2
0
As your code sorts an array, if you print something inside any bracket it will show the process, not the answer. try to print the array after the last bracket.
当你的代码对数组进行排序时,如果你在任何括号内打印东西,它将显示这个过程,而不是答案。尝试在最后一个括号之后打印数组。
int[] intArray={32,42,1,23,56,75,32,23};
int temp = 0;
for(int i=0; i < intArray.length; i++){
for(int j=1; j < intArray.length; j++){
if(intArray[j-1] > intArray[j]){
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
for( int i=0; i<intArray.length; i++ ){
System.out.println(intArray[i]);
}
I'm not a native english speaker so I apologize if there's any grammatical error in my words.
我不是英语母语的人,所以我道歉,如果我的话有语法错误。