我創建了以前使用冒泡方法適用於已存在的列表編號排序,但是,我有試圖操縱這個程序,以便允許難度程序用戶輸入用戶輸入要排序的數字列表。到目前爲止,我有:修改冒泡程序與
import java.util.Scanner;
public class MedianValue {
public static void main(String[] args) {
//use scanner to input list of numbers to sort
Scanner scan = new Scanner(System.in);
int[] numbers = new int[] {scan.nextInt()};
//nested for loop
//outer loop just iterating
//inner loop going through and flipping
//checking if out of order (if statement)
int counter = 0;
//outer loop: keep doing this until it's sorted
for(int i = 0; i < numbers.length - 1; i = i + 1)
//put in a inner loop number.length times minus one because we don't want to swap the last element
for(counter = 0; counter < numbers.length - 1; counter = counter + 1)
{
if (numbers [counter] > numbers [counter + 1])
{
int temporary = numbers [counter];
numbers [counter] = numbers [counter + 1];
numbers [counter + 1] = temporary;
}
}
for(int i =0; i < numbers.length; i = i + 1)
{
System.out.print(numbers[i] + " ");
}
}
}
但是,在這個程序中,而不是排序輸入的號碼,程序只是簡單地打印由用戶輸入的第一個號碼。我不確定是否需要移動放置掃描儀功能的位置,或者在循環內添加掃描儀功能,以便按照需要對所有數字進行排序。如果情況確實如此,我就不知道該怎麼改變計劃。