import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("数字个数:");
int n=sc.nextInt();
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
int []b=new int[n];
for(int i=0;i{System.out.print("第"+(i+1)+"个数字:");
b[i]=sc.nextInt();
}
for(int i=0;i{
if(min>b[i]){min=b[i];}
if(max }
System.out.println("最小值:"+min+" 最大值:"+max);
}
}
运行结果:
数字个数:5
第1个数字:12
第2个数字:23
第3个数字:44
第4个数字:-43
第5个数字:5
最小值:-43 最大值:44
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int num = 0;
int min = 0;
int max = 0;
System.out.print("请输入一个整数(输入0结束程序):");
num = scanner.nextInt();
min = num;
max = num;
if (num == 0)
{
System.out.println("程序结束");
scanner.close();
return;
}
do
{
System.out.print("请输入一个整数(输入0结束程序):");
num = scanner.nextInt();
if (num < min && num != 0)
{
min = num;
}
if (num > max && num != 0)
{
max = num;
}
} while (num != 0);
scanner.close();
System.out.println("最大值是" + max + "最小值是" + min);
你得for循环里面有问题,你看看啊 假如b[0]=12;你开始min=max=b[0]
if(min>b[0])//12>12 肯定是false吧,所以不执行
if(maxif(12>11)没有执行吧,
if(12<11)没有执行吧,两个if判断前后矛盾,能输出么,
import java.util.*;
public Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int min = 0;
int max = 0;
System.out.print("请输入一个数:");
int num = in.nextInt();
while (num != 0) {
if (num > max) {
max = num;
} else if (min > num) {
min = num;
}
System.out.print("请再次输入数字,输入0结束:");
num = in.nextInt();
}
System.out.println("max = " + max);
System.out.println("min = " + min);
}
}
int []b=new int[n-1];
改成 int[] b=new int[n-1];