public class Test {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in)
//从键盘输入数字个数
System.out.println("请输入数字的个数")
int count = scanner.nextInt()
int[] numberArray = new int[count]
//构造数组
for(int i = 0i <counti++){
System.out.println("请输入第 " + (i + 1) + " 个数字")
int number = scanner.nextInt()
numberArray[i] = number
}
//查找最小指
int minNumberIndex = 0
int minNumber = numberArray[minNumberIndex]
for(int i = 0i <numberArray.lengthi++){
if(numberArray[i] <minNumber){
minNumberIndex = i
minNumber = numberArray[i]
}
}
System.out.println("最小的数是第 " + (minNumberIndex + 1) + " 次输入的数字,数值是 " + minNumber)
}
}
public static void main(String[] args) {int[] a=new int[]{6,8,4,9,2}
int minint max
min=max=a[0]
for(int i=1i<a.lengthi++)
{
if(min>a[i])
min=a[i]
if(max<a[i])
max=a[i]
}
int m=0,n=0
for(int s=0s<a.lengths++)
{
if(min==a[s])
{
break
}
m++
}
for(int o=0o<a.lengtho++)
{
if(max==a[o])
{
break
}
n++
}
System.out.println("最小的数为:"+min+"下标为:"+m)
System.out.println("最大的数为:"+max+"下标为:"+n)
}
java编写程序,找出一维数组中元素的最大值和最小值:
方法1:
import java.util.*public class Main{
public static void main (String args[ ]) {
int a1[ ]=new int[] {5,3,6,8,10,56}
Arrays.sort(a1)
System.out.println("最大值:"+a1[a1.length-1])
System.out.println("最小值:"+a1[0])
}
}
方法2:
public class a {public static void main(String args[]) {
int a[]={84,40,16,3,10,49,28,76,94,70}
int n
int min=a[0]
int i
int max=a[0]
for(n=0n<=9n++) {
if(max<a[n])
max=a[n]
}
for(i=0i<=9i++) {
if(min>a)
min=a
}
System.out.print("max="+max)
System.out.print("min="+min)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)