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;
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;
}
}