气泡排序模块似乎无法正常工作;输出错误列表(java)

时间:2018-12-06 13:16:21

标签: java arrays sorting

我正在编写一个程序,该程序应该输出一个数组,该数组的元素反转,该数组的和,该数组的平均值,该数组的最小值和最大值以及该数组进行排序(按选择排序的升序和降序排列)按气泡排序)。一切似乎都已检出,但模块descendingOrderBBS()的输出不正确。到目前为止,这是我的代码:

import java.util.Scanner;
import java.lang.String;


public class Main
{

public static String input;
public static int n;

public static void main(String[] args)
{
    int[] file1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int[] file2 = new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    int[] file3 = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};

    inputData();

    switch (input)
    {
        case "file1":
        {
            System.out.println("original array: ");
            printArray(file1);

            System.out.println(" ");

            System.out.println("reversed array: ");
            reverseArray(file1);

            System.out.println(" ");

            System.out.println("The sum of all elements: " + sum(file1));

            System.out.printf("The average of all elements: %.1f%n", average(file1));

            max(file1);

            min(file1);

            //System.out.printf("Array in ascending order (Selection Sort): %-10s", ascendingOrderSS(file1));

            ascendingOrderSS(file1);

            System.out.println(" ");

            descendingOrderBBS(file1);

            //System.out.printf("Array in ascending order (Bubble Sort): %-10s", ascendingOrder(file1));

            break;
        }
        case "file2":
        {
            System.out.println(file2);
            break;
        }
        case "file3":
        {
            System.out.println(file3);
            break;
        }
        default :
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("File not found, try again: ");
            inputData();

            break;
        }
    }

}

private static String inputData()
{
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Please input file: ");
    input = keyboard.nextLine();

    return input;
}


private static void printArray(int f[])
{
    for (int i = 0; i < f.length; i++)
    {
        System.out.printf("%-10s", f[i]);
    }

}

private static void reverseArray(int f[])
{
    int end = f.length - 1;

    for (int start = 0; start < end; start++, end--)
    {
        int temp = f[start];
        f[start] = f[end];
        f[end] = temp;
    }

    for (int i = 0; i < f.length; i++)
    {
        System.out.printf("%-10s", f[i]);
    }

}

private static int sum(int f[])
{
    int sum = 0;

    for (int i = 0; i < f.length; i++)
    {
        sum += f[i];
    }

    return sum;
}

private static float average(int f[])
{
    float average = 0;

    int sum = 0;

    for (int i = 0; i < f.length; i++)
    {
        sum += f[i];
    }

    average = sum / f.length;

    return average;
}

private static void max(int f[])
{
    int largest = f[0];

    for (int i = 0; i < f.length; i++)
    {
        if (f[i] > largest)
        {
            largest = f[i];
        }
    }

    System.out.println("Max: " + largest);
}

private static void min(int f[])
{
    int smallest = f[0];

    for (int i = 0; i < f.length; i++)
    {
        if (f[i] < smallest)
        {
            smallest = f[i];
        }
    }

    System.out.println("Min: " + smallest);
}

private static void descendingOrderBBS(int f [])
{
    int i = 0;
    int j = 0;
    int temp = 0;

    for (i = 0; i < f.length; i++)
    {
        for (j = i + 1; j < f.length; j++)
            if (f[i] < f[j])
            {
                temp = f[i];
                f[i] = f[j];
                f[j] = temp;
            }
    }
    System.out.println("Array in descending order (Bubble Sort): ");

    for (int x = 0; x < f.length; x++)
    {
        System.out.printf("%-10s", f[x]);
    }
}

private static void ascendingOrderSS( int f [])
{
    int a = 0;
    int b = 0;
    int minIndex = 0;

    for (a = 0; a < f.length - 1; a++)
    {
        minIndex = a;

        for (b = a + 1; b < f.length; b++)
        {
            if (f[b] < f[minIndex])
            {
                minIndex = b;
            }
        }

        int temp = f[minIndex];
        f[minIndex] = f[a];
        f[a] = temp;
    }

    System.out.println("Array in ascending order (Selection Sort): ");

    for (int x = 0; x < f.length; x++)
    {
        System.out.printf("%-10s", f[x]);
    }
  }

}

这是整个程序一起运行

Please input file: file1
original array: 
1         2         3         4         5         6         7         8         9                10         
reversed array: 
10        9         8         7         6         5         4         3         2         1          
The sum of all elements: 55
The average of all elements: 5.0
Max: 10
Min: 1
Array in ascending order (Selection Sort): 
1         2         3         4         5         6         7         8         9         10         
Array in descending order (Bubble Sort): 
2         1         3         4         5         6         7         8         9         10        
Process finished with exit code 0

我尝试将变量更改为a / b而不是i / j。我将所有模块设为私有,以查看这是否有所作为,但是我尝试过的任何事情都没有做。哦,我也尝试使用不同的IDE。 这是两个排序模块一起的输出:

 ascendingOrderSS(file1);

 System.out.println(" ");

 descendingOrderBBS(file1);

Array in ascending order (Selection Sort): 
1         2         3         4         5         6         7         8         9         10         
Array in descending order (Bubble Sort): 
2         1         3         4         5         6         7         8         9         10        
Process finished with exit code 0

忽略升序模块的输出:

//ascendingOrderSS(file1);

System.out.println(" ");

descendingOrderBBS(file1);


Array in descending order (Bubble Sort): 
10        9         8         7         6         5         4         3         2         1         
Process finished with exit code 0

我不确定为什么DescendingOrderBBS()正在更改。我已经完成了最后一个项目,直到凌晨5:12。

所有固定:

Array in ascending order (Selection Sort): 
1         2         3         4         5         6         7         8            9         10         
Array in descending order (Bubble Sort): 
10        9         8         7         6         5         4         3         2         1         
Process finished with exit code 0

1 个答案:

答案 0 :(得分:1)

对于您的第一个问题,为什么打印5.0而不打印5.5是因为sum / f.length是整数的除法,并且.之后的值将被删除,但是转向将sum植入float或将sum投射到float将解决此问题。

您当前的代码(显示1.0

float average = 3 / 2;
System.out.println(average);

修复程序(此打印为1.5):

float sum = (float)3 / 2;
System.out.println(sum);

32是代码中的变量sumf.length