【发布时间】:2018-11-27 01:40:43
【问题描述】:
我想编写一个程序,如果输入为true,则添加a 和b,如果输入为false,则从a 中减去b。另外,当为ArrayList时,如果输入为true,则取最大值,如果输入为false,则取最小值。
public class Source7_3 {
public static void main(String[] args) {
OverLoading mm = new OverLoading();
int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int[] a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
但我无法得到结果值.. 我怎么才能得到它? 感谢您的帮助!
【问题讨论】:
-
您甚至没有调用
dist函数。在 print 语句中,您正在打印字符串“dist”而不是调用方法。 -
另外,在第二种方法中,您可能正在尝试做
return b ? max : min。
标签: java overloading