java - 无法使用 Java 中的交换方法对 int 数组进行排序

标签 java arrays sorting

我正在尝试输入数组的长度,然后输入数组元素。我能够检查案例 1 中的最大值,但我在案例 2 中将数组从最小数字重新排序到最大数字,但我无法做到这一点。

这是我的代码:

package peopleinfo;

import java.util.Scanner;

public class mainapp
{
  public static void main(String[] args)
  {
    Scanner sc1 = new Scanner(System.in);
    System.out.println("please input the length of the array:");
    arraybuilder(sc1.nextInt());
  }

  public static void arraybuilder(int i)
  {
    Scanner sc = new Scanner(System.in);
    int[] array = new int[i];
    for (int j = 0; j < array.length; j++)
    {
      int o = j;
      System.out.println("please input the" + ++o + " number");
      array[j] = sc.nextInt();
    }
    for (int j2 = 0; j2 < array.length; j2++)
      System.out.print(array[j2] + "\t");
    System.out.println("what do you want to do ?\n 1 check the biggest number\n2 sort the array from the smallest\nterminate the app");
    int dwc = sc.nextInt();
    switch (dwc)
    {
      case 1:
        int k = 0;
        int max = 0;
        for (int j = 0; j < array.length; j++)
        {
          if (array[j] > k)
          {
            max = j;
            k = array[j];
          }
        }
        System.out.println("the largest number is " + array[max]);
        break;

      case 2:
        int temp;
        boolean fixed = false;
        while (fixed = false)
        {
          fixed = true;
          for (int u = 0; u < array.length - 1; u++)
          {
            if (array[u] > array[u + 1])
            {
              temp = array[u + 1];
              array[u + 1] = array[u];
              array[u] = temp;
              fixed = false;
            }
          }
        }
        for (int j = 0; j < array.length; j++)
        {
          System.out.print(array[j] + "\t");
        }
        break;
      default:
        System.out.println("Error, please rerun the code:)");
        break;
    }
  }
}

我不明白我做错了什么?

最佳答案

 case 2:
                int temp;
                boolean fixed = false;
               while (fixed == false) {    // See the change here
                    fixed = true;
                    for (int u = 0; u < array.length - 1; u++) {
                        if (array[u] > array[u + 1]) {
                            temp = array[u + 1];
                            array[u + 1] = array[u];
                            array[u] = temp;
                            fixed = false;
                        }
                    }
                }
                for (int j = 0; j < array.length; j++) {
                    System.out.print(array[j] + "\t");
                }

                break;

在你的情况2中,while()循环有fixed = false

应该是fixed == false,即while(fixed==false)

有关更多信息,请点击链接... Using the assignment operator instead of the equality operator

关于java - 无法使用 Java 中的交换方法对 int 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153160/

相关文章:

javascript - 将 JSON 字符串反序列化为列表数组

C++ 字母插入排序

java - 从 JdbcTemplate 查询 BLOB 时出现连接关闭错误

java - 与数据库交互时出现 ClassNotFoundException 错误

java - 连接 GUI (Swing) 和业务逻辑

javascript - 在javascript中按多个键和多个顺序排序

linux - 查找文件中 0 到 1000 之间的不同组

java - Camunda BPM 引擎中的 Thrift 调用

javascript - Array.from 不能作为 Array#map 中的直接回调函数

python - 如何在 numpy 中使用二维数组构造对角线数组?