Description
利用数组和函数重载求5个数最大值(分别考虑整数、单精度、长整数的情况)。
Input
分别输入5个int型整数、5个float 型实数、5个long型正整数。
Output
分别输出5个int型整数的最大值、5个float 型实数的最大值、5个long型正整数的最大值。
Sample
Input
11 22 666 44 55
11.11 22.22 33.33 888.88 55.55
1234567 222222 333333 444444 555555
Output
666
888.88
1234567
import java.util.Scanner;
class Math1 {
//方法的重载
//两方法的访问权限相同,名称相同,参数不同
int n=5;
public int f(int a[]) {
int max=0;
for(int i=0;i<n;i++)
if(max<a[i])
max=a[i];
return max;
}
public float f(float a[]) {
float max=0;
for(int i=0;i<n;i++)
if(max<a[i])
max=a[i];
return max;
}
public long f(long a[]) {
long max=0;
for(int i=0;i<n;i++)
if(max<a[i])
max=a[i];
return max;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Math1 math = new Math1();
int a[]=new int[5];
float b[]=new float[5];
long c[]=new long[5];
for(int i=0;i<5;i++)
a[i]=input.nextInt();
for(int i=0;i<5;i++)
b[i]=input.nextFloat();
for(int i=0;i<5;i++)
c[i]=input.nextLong();
System.out.println(math.f(a));
System.out.println(String.format("%.2f",math.f(b)));
System.out.println(math.f(c));
input.close();
}
}
本文介绍了一个使用数组和函数重载来寻找五个数中最大值的方法。该方法适用于整数、单精度浮点数和长整数类型的数据。通过示例展示了如何输入不同类型的数据并输出相应的最大值。
3530

被折叠的 条评论
为什么被折叠?



