输入一批整数,输出最大值和最小值,输入0结束
方法一:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num=input.nextInt();
int max=num,min=num;
do{
System.out.println("请输入一个整数:");
num=input.nextInt();
if(max<num){
max=num;
}else if(min>num && num!=0){
min=num;
}
}while(num!=0);
System.out.println("最大值是:"+max);
System.out.println("最小值是:"+min);
System.out.println("循环结束");
}
方法二:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入数字:");
int num=input.nextInt();
int max=num,min=num;
while(num!=0){
System.out.println("请输入数字:");
num=input.nextInt();
if(max<num){
max=num;
}else if(min>num && num!=0){
min=num;
}
}
System.out.println("最大值是:"+max+"\n最小值是:"+min);
System.out.println("循环结束!");
}