【问题标题】:Bubble sort Arrays of User Input of Int valuesInt 值的用户输入的冒泡排序数组
【发布时间】:2019-01-19 15:19:17
【问题描述】:

我正在尝试解决我在使用此代码时遇到的两个问题。首先,我想将我的整数输入设置为最大值 100,其次我试图获取输入的整数数组并按绝对值对它们进行排序。所以如果用户使用这个程序,我希望他们只能输入 99 来表示他们想要排序的整数个数。当它最终被排序而不是降序或升序时,我希望它是这样的; -1,2,-6,10,-2​​0。

 public static void main(String args[])
{
   int n, i, j, temp;
   int arr[] = new int[50];
   Scanner scan = new Scanner(System.in);

   //Input area for user data, setting the number of integers to sort
   System.out.print("Enter Total Number of Integers you would like to sort : ");
   n = scan.nextInt();

   for (n=0, n < 100, n++)

   {
   }

   //Input area for user data, asking user to input the ints into an array for sorting
   System.out.print("Enter " +n+ " Numbers : ");
   for(i=0; i<n; i++)
   {
       arr[i] = scan.nextInt();
   }

   // Sorting Array using Bubble Sort Technique
   for(i=0; i<(n-1); i++)
   {
       for(j=0; j<(n-i-1); j++)
       {
           if(arr[j] > arr[j+1])
           {
               temp = arr[j];
               arr[j] = arr[j+1];
               arr[j+1] = temp;
           }
       }
   }

   // Command line output from user, sorted by absolute value      
   System.out.print("Sorted List : \n");
   for(i=0; i<n; i++)
   {
       System.out.print(java.lang.Math.abs(arr[i])+ "  ");
   }
 }
}

【问题讨论】:

  • 有错误吗?
  • 你没有问任何问题。
  • 我问我如何限制用户在初始组中输入 99 或更小的整数进入我的数组的能力。以及如何确保列表被排序为由绝对值形成的列表,而不是降序或升序。
  • 正如代码所示,没有没有错误,但我有两个问题需要帮助,因为我不知道该怎么做。我尝试使用 java.lang.Math.abs() 作为我的绝对值,但它似乎没有按预期工作。
  • 好吧,你需要添加代码来做到这一点:读取整数,检查它是否小于 100,如果不是,打印一个错误,再问一次。我相信你已经学会了循环和 if。使用它们。我也相信你知道什么是绝对值。因此,与其比较数字,不如比较它们的绝对值。尝试一些东西。我们不会做你的功课。告诉我们您在没有发布代码的情况下尝试了某些东西,并告诉我们“它似乎没有按预期工作”太含糊了。

标签: java arrays multidimensional-array bubble-sort


【解决方案1】:

我有一些建议(大部分是次要的)和一个可能的解决方案:

  1. 代码风格很重要!使用appropriate indentation and spacing

  2. 不要在函数顶部声明一堆索引变量。这对 C89 来说很好,但在 Java 中不是必需的。将变量保持在尽可能窄的范围内可以防止错误。

  3. 在排序例程中使用 Math.abs() 而不是打印代码来获得所需的结果。

  4. 如果您提前不确定长度,请考虑使用 ArrayList 而不是原始数组。复制填充后的数组以消除未使用的尾部是另一种合理的方法。否则,arr.length 是不准确的,并且您将被困在依赖随附的长度变量中,这可能会在以后导致错误。

  5. 考虑使用函数来模块化您的代码,尤其是对于像排序这样明显可重用和模块化的组件。

  6. 考虑使用冒泡排序以外的其他方法,尽管这种方法适用于此类教学示例,但效率低下。像Arrays.sort() 这样的内置排序库是工业级的方法。

  7. for (n=0, n &lt; 100, n++) 是语法错误。使用; 分隔for。此行还会用 0 覆盖您刚刚从用户那里收集的 n 值。使用 i 在此处计数或执行 n-- 直到 n == 0 迭代 n 次。

  8. 如果您允许您使用最多 99 个整数,请使用长度为 99 而不是 50 的数组,这样您就可以容纳所有内容。

  9. nextInt() 会在用户输入非整数时崩溃。查看error handling处理此类情况。

这里有一些代码:

import static java.lang.System.out;
import java.util.*;

class Main {
    public static void main(String args[]) {
        int arr[] = new int[99];
        Scanner scan = new Scanner(System.in);

        out.print("Enter total number of integers you would like to sort: ");
        int n = scan.nextInt();

        while (n < 1 || n > 99) {
            out.print("Please enter a number between 1 and 99: ");
            n = scan.nextInt();
        }

        for (int i = 0; i < n; i++) {
            out.print("Input a number less than 100: ");
            arr[i] = scan.nextInt();

            if (arr[i] > 100) { 
                out.println("That number was over 100. Try again.");
                i--; 
            }
        }

        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (Math.abs(arr[j]) > Math.abs(arr[j + 1])) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        out.println("Sorted List :");

        for (int i = 0; i < n; i++) {
            out.print(arr[i] + "  ");
        }
    }
}

样品运行

Enter total number of integers you would like to sort:  6
Input a number less than 100:  1
Input a number less than 100:  -9
Input a number less than 100:  3
Input a number less than 100:  4
Input a number less than 100:  -5
Input a number less than 100:  6
Sorted List : 
1  3  4  -5  6  -9 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2012-10-20
    • 2018-03-29
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多