英文:
Not sorting an array with bubble sorting
问题
对不起,初学者问题真是抱歉。我遇到了使用冒泡排序对数组进行排序的问题:
class Search {
public static void main(String args[]) {
int temp;
boolean fixed = false;
while (fixed = false) {
fixed = true;
int number[] = {6, 7, 3, 2, 9};
for (int i = 0; i < number.length - 1; i++) {
if (number[i] > number[i + 1]) {
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
for (int i = 0; i < number.length; i++) {
System.out.println(number[i]);
}
}
}
}
我应该怎么做来解决我的问题呢?
英文:
Sorry for the beginner question, I faced a problem sorting an array using bubble sorting:
class Search {
public static void main(String args[]) {
int temp;
boolean fixed = false;
while (fixed = false) {
fixed = true;
int number[] = {6, 7, 3, 2, 9};
for (int i = 0; i < number.length - 1; i++) {
if (number[i] > number[i + 1]) {
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
for (int i = 0; i < number.length; i++) {
System.out.println(number[i]);
}
}
}
}
what can I do to fix my problem?
答案1
得分: 1
你有一个条件问题(fixed = false),这个条件始终为真,你应该在循环后打印结果,而且你将数字排序错误(从小到大,你本意是相反的)。你还需要在循环之外定义数组。
int temp;
boolean fixed = false;
int number[] = {6, 7, 3, 2, 9};
while (!fixed) {
fixed = true;
for (int i = 0; i < number.length - 1; i++) {
if (number[i] < number[i + 1]) {
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
}
for (int i = 0; i < number.length; i++) {
System.out.println(number[i]);
}
英文:
You have a condition problem (fixed = false) which is always true, your should print your result after your loop and you sorted the numbers wrong (small to bigger, you intended the other way). You also need to define your array outside the loop.
int temp;
boolean fixed=false;
int number[]= {6, 7, 3, 2, 9 };
while(!fixed) {
fixed = true;
for(int i=0; i<number.length-1; i++) {
if(number[i] < number[i+1]) {
temp = number[i + 1];
number[i+1] = number[i];
number[i] = temp;
fixed = false;
}
}
}
for(int i=0; i < number.length; i++) {
System.out.println(number[i]);
}
答案2
得分: -1
1. 1)修复 while (fixed == false)
2) 修复数组初始化
在 while 循环外部
3) 在 while 循环外添加 sysout
public class Test {
public static void main(String[] args) {
int temp;
boolean fixed = false;
int number[] = {6, 7, 3, 2, 9};
while (fixed == false) {
fixed = true;
for (int i = 0; i < number.length - 1; i++) {
if (number[i] > number[i + 1]) {
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
}
for (int i = 0; i < number.length; i++) {
System.out.println(number[i]);
}
}
}
输出结果:
2
3
6
7
9
英文:
- 1)Fixed while (fixed == false)
- Fixed array initialization
outside while loop - added sysout outside of while loop
public class Test {
public static void main(String[] args) {
int temp;
boolean fixed = false;
int number[] = {6, 7, 3, 2, 9};
while (fixed == false) {
fixed = true;
for (int i = 0; i < number.length - 1; i++) {
if (number[i] > number[i + 1]) {
temp = number[i + 1];
number[i + 1] = number[i];
number[i] = temp;
fixed = false;
}
}
}
for (int i = 0; i < number.length; i++) {
System.out.println(number[i]);
}
}
Output:
2
3
6
7
9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论