java,任意输入一串数字,要求算出最大值最小值。 请用最简单的方法
Enteranumber(anon-numbertoexit):2Enteranumber(anon-numbertoexit):4Enteranumber(anon-n...
Enter a number (a non-number to exit): 2
Enter a number (a non-number to exit): 4
Enter a number (a non-number to exit): 8
Enter a number (a non-number to exit): -2
Enter a number (a non-number to exit): -4
Enter a number (a non-number to exit): -3
Enter a number (a non-number to exit): x
Max of the numbers: 8
Min of the numbers: -3 展开
Enter a number (a non-number to exit): 4
Enter a number (a non-number to exit): 8
Enter a number (a non-number to exit): -2
Enter a number (a non-number to exit): -4
Enter a number (a non-number to exit): -3
Enter a number (a non-number to exit): x
Max of the numbers: 8
Min of the numbers: -3 展开
3个回答
展开全部
设置两个变量 max min
输入的时候就比较一下,然后获取对应的max 和 min
最后的结果就出来了
如:int max=0,min=0
int sr=new Integer(输入);
if(sr>max)max=sr;
if(sr<min)min=sr;
输入的时候就比较一下,然后获取对应的max 和 min
最后的结果就出来了
如:int max=0,min=0
int sr=new Integer(输入);
if(sr>max)max=sr;
if(sr<min)min=sr;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Number {
public static void main(String[] args) {
String input = null;
Scanner scanner = new Scanner(System.in);
int max = 0;
int min = 0;
boolean isFirst = true;
while(true){
System.out.print("Enter a number (a non-number to exit): ");
input = scanner.next();
try{
int value = Integer.parseInt(input);
if(isFirst){
max = value;
min = value;
isFirst = false;
}else{
if(value > max){
max = value;
}else{
min = value;
}
}
}catch(NumberFormatException exp){
System.out.println("Max of the numbers : " + max);
System.out.println("Min of the numbers : " + min);
break;
}
}
}
}
--------------testing
Enter a number (a non-number to exit): 2
Enter a number (a non-number to exit): 4
Enter a number (a non-number to exit): 8
Enter a number (a non-number to exit): -2
Enter a number (a non-number to exit): -4
Enter a number (a non-number to exit): -3
Enter a number (a non-number to exit): x
Max of the numbers : 8
Min of the numbers : -3
public class Number {
public static void main(String[] args) {
String input = null;
Scanner scanner = new Scanner(System.in);
int max = 0;
int min = 0;
boolean isFirst = true;
while(true){
System.out.print("Enter a number (a non-number to exit): ");
input = scanner.next();
try{
int value = Integer.parseInt(input);
if(isFirst){
max = value;
min = value;
isFirst = false;
}else{
if(value > max){
max = value;
}else{
min = value;
}
}
}catch(NumberFormatException exp){
System.out.println("Max of the numbers : " + max);
System.out.println("Min of the numbers : " + min);
break;
}
}
}
}
--------------testing
Enter a number (a non-number to exit): 2
Enter a number (a non-number to exit): 4
Enter a number (a non-number to exit): 8
Enter a number (a non-number to exit): -2
Enter a number (a non-number to exit): -4
Enter a number (a non-number to exit): -3
Enter a number (a non-number to exit): x
Max of the numbers : 8
Min of the numbers : -3
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写一个求最值的函数即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询