python生成包含500以內的10個隨機數的列表?
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
@authorHeYiding
publicclassScannerDemo{
publicstaticvoidmain(String[]args){
Stringprompt="請輸入任意多個數字,用空格隔開:";
List<Integer>ints=readNumbers(prompt);
Integermax=null,min=null;
for(Integeri:ints){
if(max==null||i>max){
max=i;
}elseif(min==null||i<min){
min=i;
}
}
System.out.println("最大值:"+max+",最小值:"+min);
}
讀取用戶輸入的數字,數字以空格隔開
@paramprompt提示信息
@return用戶輸入的數字
privatestaticList<Integer>readNumbers(Stringprompt){
List<Integer>result=newArrayList<Integer>();
System.out.println(prompt);
Scannerscanner=newScanner(readLine());
while(scanner.hasNextInt()){
result.add(scanner.nextInt());
}
returnresult;
}
privatestaticStringreadLine(){
ScannerlineReader=newScanner(System.in);
Stringline=lineReader.nextLine();
returnline;
}
}