62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class FindNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int currentNum=0,maxNum=0,minNum=0;
currentNum=sc.nextInt();
minNum=currentNum;
maxNum=currentNum;
while(currentNum!=-99){
if(currentNum>maxNum){
maxNum=currentNum;
}
if(currentNum<minNum){
minNum=currentNum;
}
currentNum=sc.nextInt();
}
sc.close();
System.out.println("max:"+maxNum+"-------min:"+minNum);
}
}
试试看import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String input = "23 33 45 adfb 123 132 -99 23 abc";
input = input.substring(0, input.indexOf("-99"));
Integer min = null;
Integer max = null;
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
Integer value = Integer.valueOf(matcher.group(0));
if (min == null) {
min = value;
max = value;
}
min = value < min ? value : min;
max = value > max ? value : max;
}
System.out.println("Min: " + min + ", Max: " + max);
}
}