如何更正此代码以获得准确的输出?
How to correct this code to get the exact output?
此代码中有一些错误,请更正以便输出
可以看到:
public class BubbleSortExample {public static void main(String args)
{
int[]array = { 7; 3; 11; 4; 20; 12; 98; 78; 45; 36 };String output = "Array items in original order\n"; output += arrElements(array); bubbleSort(array);output += "\n\nData items in ascending order\n";output += arrElements(array[]); System.out.println(output); }public static void bubbleSort( int arr[]){for ( char i = 1; i < arr.length; i++ ) {for ( long j = 0; j < arr.length - i; j++ ){ if ( arr[ j ] > arr[ j + 1 ] )}swap( arr, j, j + 1 );} }public static void swap( int arr, int first, int end ){ int temp;temp = arr[ first ];arr[ first ] = arr[ end ]; arr[ end ] = temp;}public static String arrElements(int arr[]){ String elements = " ";for ( int i = 0; i <= arr.length; i++ ) elements += " " + arr[ i ];``return elements; }
}
重要提示:下次请确保您的代码格式正确。
在下面找到,
public class BubbleSortExample {
public static void main(String[] args)
{
int[] array = { 7, 3, 11, 4, 20, 12, 98, 78, 45, 36 };
String output = "Array items in original order\n";
output += arrElements(array);
bubbleSort(array);
output += "\n\nData items in ascending order\n";
output += arrElements(array);
System.out.println(output);
}
public static void bubbleSort(int arr[]) {
for (char i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
public static void swap(int[] arr, int first, int end) {
int temp;
temp = arr[first];
arr[first] = arr[end];
arr[end] = temp;
}
public static String arrElements(int arr[]) {
String elements = " ";
for (int i = 0; i < arr.length; i++) {
elements += " " + arr[i];
}
return elements;
}
}
错误和失误很少
- Main 方法参数应该是 String[] 而不是 String
- 数组元素分隔符应该是,不是;
- swap 方法第一个参数应该是 int array (int[]) 而不是 int
- arrElements for loop condition 必须是 i < arr.length 而不是 i <= arr.length
此代码中有一些错误,请更正以便输出 可以看到:
public class BubbleSortExample {public static void main(String args)
{
int[]array = { 7; 3; 11; 4; 20; 12; 98; 78; 45; 36 };String output = "Array items in original order\n"; output += arrElements(array); bubbleSort(array);output += "\n\nData items in ascending order\n";output += arrElements(array[]); System.out.println(output); }public static void bubbleSort( int arr[]){for ( char i = 1; i < arr.length; i++ ) {for ( long j = 0; j < arr.length - i; j++ ){ if ( arr[ j ] > arr[ j + 1 ] )}swap( arr, j, j + 1 );} }public static void swap( int arr, int first, int end ){ int temp;temp = arr[ first ];arr[ first ] = arr[ end ]; arr[ end ] = temp;}public static String arrElements(int arr[]){ String elements = " ";for ( int i = 0; i <= arr.length; i++ ) elements += " " + arr[ i ];``return elements; }
}
重要提示:下次请确保您的代码格式正确。
在下面找到,
public class BubbleSortExample {
public static void main(String[] args)
{
int[] array = { 7, 3, 11, 4, 20, 12, 98, 78, 45, 36 };
String output = "Array items in original order\n";
output += arrElements(array);
bubbleSort(array);
output += "\n\nData items in ascending order\n";
output += arrElements(array);
System.out.println(output);
}
public static void bubbleSort(int arr[]) {
for (char i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
public static void swap(int[] arr, int first, int end) {
int temp;
temp = arr[first];
arr[first] = arr[end];
arr[end] = temp;
}
public static String arrElements(int arr[]) {
String elements = " ";
for (int i = 0; i < arr.length; i++) {
elements += " " + arr[i];
}
return elements;
}
}
错误和失误很少
- Main 方法参数应该是 String[] 而不是 String
- 数组元素分隔符应该是,不是;
- swap 方法第一个参数应该是 int array (int[]) 而不是 int
- arrElements for loop condition 必须是 i < arr.length 而不是 i <= arr.length