【问题标题】:How can I get the result value in java overloading如何在java重载中获取结果值
【发布时间】:2018-11-27 01:40:43
【问题描述】:

我想编写一个程序,如果输入为true,则添加ab,如果输入为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


【解决方案1】:

我认为您正在尝试调用这些方法,但目前您只是附加 Strings

System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");

应该是

System.out.println(mm.dist(mm.a, mm.b, true);

因为字段 ab 是类的一部分,所以没有必要传递它们

【讨论】:

  • 谢谢!但是我怎样才能得到arraylist的最大值和最小值呢? System.out.println(mm.dist(mm.a[], true);我写的和你一样,但在这种情况下,它似乎是错误的方式..哈哈
  • int max, min; 移动为字段 - 方法完成后,您可以获取它们的值。
【解决方案2】:
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) {
        int max, min;
        max = min = a[0];
    for (int j = 0; j < a.length; j++) {
        if (max < a[j])
            max = a[j];
        if (min > a[j])
            min = a[j];
    }
    return  d == true ? max : min;
}
}

public class HelloWorld{

    public static void main(String[] args) {
    OverLoading mm = new OverLoading();

    int[] a = new int[10];
    System.out.println("\n");
    for (int i = 0; i < a.length; i++)
    {
        a[i] = (int) (Math.random() * 100) + 1;
        System.out.println(a[i]);
    }
    System.out.println("\n");
    System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
    System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
    System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
    System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2012-02-05
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
相关资源
最近更新 更多