用JAVA编写一个程序,计算一维数组中的最大值、最小值及其差值

2024-05-02 06:04

1. 用JAVA编写一个程序,计算一维数组中的最大值、最小值及其差值

public class Max {
 public static void main(String[] args) {
  int[] y= {0,9,4,8,2,12,444,5,-1,-22,45};
  
  int max = m(y);
  int min = mm(y);
  
  System.out.println("最大值 "+max);
  System.out.println("最小值 "+min);
  System.out.println("差值   "+max-min);
 }
 public static int m(int[] p){
  int max=0;
  for (int i=0;i<p.length;i++){
   if(p[i]>=max){
    max=p[i];
   }
  }
  return max;
 }
 public static int mm(int[] p){
  int min=p[0];
  for (int i=1;i<p.length;i++){
   if(p[i]<=min){
    min=p[i];
   }
  }
  return min;
 }

}

用JAVA编写一个程序,计算一维数组中的最大值、最小值及其差值

2. 用java程序写出一个程序求3个数中的最大值

int a=1,b=2,c=3   简写:int maxNum= a>b?a>c?a:c:b>c?b:c;
System.out.print(maxNum); //输出最大值
也可以用程序判断
if(a>b){
        if(a>c){
                  Ststem.out.print(a);//最大值
        }else{
                  Ststem.out.print(c);//最大值
        }
}else{
       if(b>c){
                 Ststem.out.print(b);//最大值
       }else{
                 Ststem.out.print(c);//最大值
       }
}

上面的逻辑有点乱,初学者一般这么写。还可以将三个数放到一个数组中,进行排序,排序方法很多。

3. java编程:计算一个一维数组中的最大值和最小值及其差值。

public class Test {	public static void main(String[] args) {		int[] n = {325,2362,123,6,123,-8,-235,23};		getMaxMin(n);	}			static void getMaxMin(int[] n){		int min = Integer.MAX_VALUE;		int max = Integer.MIN_VALUE;		for(int i=0;in[i]){				min = n[i];			}			if(max<n[i]){				max = n[i];			}		}		System.out.println("最大值:"+max);		System.out.println("最小值"+min);		System.out.println("相差:"+(max-min));	}}

java编程:计算一个一维数组中的最大值和最小值及其差值。

4. 编写JAVA程序,从十个数中找出最大值

import java.util.Scanner;

public class Test {
	
	public static void main(String args[]){
		
		int num[]=new int[10];
		int max;
		Scanner scan=new Scanner(System.in);
		System.out.print("请输入10个数字:");
		for(int i=0; i<10; i++){
			num[i]=scan.nextInt();
		}
		max=num[0];
		for(int i=1; i<10; i++){
			if(max<num[i]) max=num[i];
		}
		System.out.println("最大值是"+max);
	}
       
}

5. java编程:计算一个一维数组中的最大值和最小值及其差值。

import java.util.Arrays;

public class Test1 {
    
    public static void main(String[] args) {
        int[] ary = {23, 43, 21, 67, 33, 89, 70};
        Arrays.sort(ary);
        int max = ary[ary.length-1];
        int min = ary[0];
        System.out.println("最大值是:" + max);
        System.out.println("最小值是:" + min);
        System.out.println("差值是:" + (max - min));
    }

}

运行结果
最大值是:89
最小值是:21
差值是:68

java编程:计算一个一维数组中的最大值和最小值及其差值。

6. java编写程序,求若干个求最大值的方法Max……

import java.util.Arrays;
public class test {

 public static int Max(int a ,int b ,int c ){ 
  return Math.max(Math.max(a, b), c);
 }
 
 public static float Max(float a ,float b ,float c ){ 
  return Math.max(Math.max(a, b), c);
 }
 
 public static double Max(double a ,double b ,double c ){ 
  return Math.max(Math.max(a, b), c);
 }
 
 public static int Max(String a ,String b ,String c ){ 
  Integer a1 = new Integer(a);
  Integer a2 = new Integer(b);
  Integer a3 = new Integer(c);
  return Math.max(Math.max(a1, a2), a3);
 }
 
 public static int Max(int[] arr){ 
  Arrays.sort(arr);
  return arr[arr.length-1];
 }
}
 
 
使用的是方法的重载。

7. 编写一个Java应用程序,计算并输出一维数组(9.8,12,45,67,23,1.98,2.55,45)中的最大值和最小值。

public class Test { 
 public static void main(String[] args) {
  double []d={9.8,12,45,67,23,1.98,2.55,45};  
  double max=0;//当前最大值
  double min=0;//当前最小值
  for (int i = 0; i < d.length; i++) {
   if(i==0)  //假设第一个最大,最小
    max=min=d[0];   
   if(d[i]>max)      //大于最大
    max=d[i];
   if(d[i]<min)     //小于最小
    min=d[i];
  }
  System.out.println("最大值:"+max);
  System.out.println("最小值:"+min);
 }
}

编写一个Java应用程序,计算并输出一维数组(9.8,12,45,67,23,1.98,2.55,45)中的最大值和最小值。

8. Java如何用两个类算最大值

求两个数中的大的吗?
java自带的Math类中就有方法的   
直接  
int i = 1,j=2;
Math.max(i, j) 得到的值就是2;
最新文章
热门文章
推荐阅读