这通常是如何完成的,但它必须是递归的,没有 for、do-while 和 while 循环。仅 if 语句。
import java.util.ArrayList;
import java.util.Scanner;
public class arrayex1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<Integer>();
System.out.println("Enter numbers: ");
for (int i = 0; i < 10; i++) {
int num = input.nextInt();
numbers.add(num);
}
for (int i = 0; i < numbers.size(); i++) {
if (numbers.get(findMin(numbers)) == i) { // If the 'smallest' index value is equal to i.
System.out.println(numbers.get(i) + " <== Smallest number");
} else {
System.out.println(numbers.get(i));
}
}
}
public static int findMin(ArrayList<Integer> n) {
int min = 0; // Get value at index position 0 as the current smallest.
for (int i = 0; i < n.size(); i++) {
if (n.get(i) < min) {
min = i;
}
}
return min;
}
}