62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author HeYiding
*/
public class ScannerDemo {
public static void main(String[] args) {
// 获取用户输入
String prompt = "请输入任意多个数字,用空格隔开:";
List<Integer> ints = readNumbers(prompt);
// 查找最大最小值
Integer max = null, min = null;
for (Integer i : ints) {
if (max == null || i > max) {
max = i;
} else if (min == null || i < min) {
min = i;
}
}
System.out.println("最大值: " + max + ", 最小值: " + min);
}
/**
* 读取用户输入的数字,数字以空格隔开
*
* @param prompt 提示信息
*
* @return 用户输入的数字
*/
private static List<Integer> readNumbers(String prompt) {
List<Integer> result = new ArrayList<Integer>();
System.out.println(prompt);
Scanner scanner = new Scanner(readLine());
while (scanner.hasNextInt()) {
result.add(scanner.nextInt());
}
return result;
}
private static String readLine() {
Scanner lineReader = new Scanner(System.in);
String line = lineReader.nextLine();
return line;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Max_Min {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int count=1;
List<Integer> list=new ArrayList<Integer>();
while(count<11){
System.out.println("请输入第"+count+"个数字");
list.add(scan.nextInt());
count++;
}
Collections.sort(list);
System.out.println("Min:"+list.get(0));
System.out.println("Max:"+list.get(list.size()-1));
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Max_Min {
public static void main(String[] args){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int max=0;
int min=10000000;
for(int i=0;i<10;i++){
try {
int c=Integer.parseInt(bf.readLine());
if(c>max){
max=c;
}
if(c<min){
min=c;
}
//System.out.println();
}
catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Max number:"+max);
System.out.println("Min number:"+min);
}
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
max=min=Integer.parseInt(br.readLine());
} catch (IOException e) { }
for(int i=0;i<10;i++){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
max=min=Integer.parseInt(br.readLine());
} catch (IOException e) { }
}