我需要允许用户键入100个数字,即100个输入,然后从其中输入最小的数字。键入100个.nextInt()行是非常低效的,我认为我可以使用正好100个输入的数组,然后一旦找到,就找到最小值并打印出来。但是我不知道该怎么做,那么简单的方法是什么呢?谢谢
答案 0 :(得分:1)
您可以在不使用数组的情况下进行操作,看看如何操作。
git log --decorate
答案 1 :(得分:1)
尝试此代码示例。 我在计算机上运行了它,并且可以正常工作。
GET
希望这会有所帮助:-)
答案 2 :(得分:0)
您不需要数组,此示例使用递归函数/方法:
import java.util.Scanner;
public class Code{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int min = prompt(sc, 1, 5); /* prompts for 5 values, change as required */
sc.close();
System.out.printf("Minium value is: %d%n", min);
}
private static int prompt(Scanner sc, int count, int times){
System.out.printf("Enter number %d of %d: ", count, times);
int n = sc.nextInt();
if(count == times){
return n;
}
return Math.min(n, prompt(sc, (1 + count), times));
}
}
答案 3 :(得分:0)
感谢您提出此问题,而无需其他答案中说明的任何数组,但是如果您要使用数组,请将其声明为
int arr[]=new int[100];
使用for循环在其中输入值,
然后申请
Arrays.sort(arr);
arr [0]将是最小值元素。