args 变量中整数的最大和最大差

发布于 2024-11-28 14:26:54 字数 1286 浏览 1 评论 0原文

我正在编写一段代码,计算 args 变量中的最大数字以及 args 中最高和最低整数之间的最大差异。

目前我的代码如下所示:

public int max(int [] args) {//array of ints
    int m = args[0]; // first element

    //initialisation; condition; update
    for (int j = 1; j < args.length; ++j) {
        // statement in a block:
    if (m < args[j]) {
        m = nums[j];
        // if m is less than the j-th element
        // then store this new smaller value
        }
    }
    return m;

}

    public int min(int [] args) {//array of integerss
        int mi = nums[0]; // first element

        //initialisation; condition; update
        for (int j = 1; j < args.length; ++j) {
            // statement in a block:
            if (mi > args[j]) {
                mi = args[j];
            // if m is greater than the j-th element
            // then store this new largest value
        }
    }
        return mi;

} //通过将数字之和除以计数来计算平均值

    public void main(String [] args) {
        System.out.println(args[0]);

        SimpleCalc fm = new SimpleCalc();
        **System.out.println(fm.max(nums));**
        **System.out.println(fm.max(nums) - fm.min(nums));**

当我使用数组时它返回值,但它似乎无法使用args进行编译。我不知道如何解决这个问题。

I am writing a code that calculates the maximum number in my args variable as well as the largest difference between the highest and lowest integer in args.

Currently my code looks like this:

public int max(int [] args) {//array of ints
    int m = args[0]; // first element

    //initialisation; condition; update
    for (int j = 1; j < args.length; ++j) {
        // statement in a block:
    if (m < args[j]) {
        m = nums[j];
        // if m is less than the j-th element
        // then store this new smaller value
        }
    }
    return m;

}

    public int min(int [] args) {//array of integerss
        int mi = nums[0]; // first element

        //initialisation; condition; update
        for (int j = 1; j < args.length; ++j) {
            // statement in a block:
            if (mi > args[j]) {
                mi = args[j];
            // if m is greater than the j-th element
            // then store this new largest value
        }
    }
        return mi;

}
//compute average by dividing sum of numbers over the count

    public void main(String [] args) {
        System.out.println(args[0]);

        SimpleCalc fm = new SimpleCalc();
        **System.out.println(fm.max(nums));**
        **System.out.println(fm.max(nums) - fm.min(nums));**

It was returning values when i used arrays, but it doesnt seem to compile with args. Im not sure how to fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-12-05 14:26:54

它无法编译,因为 main 方法中的 args 是一个 String 数组。您的方法采用 int 数组作为参数。因此,您应该将 Stringargs 数组转换为 int 数组,并传递这个新的 int 数组code> 到您的 minmax 方法。使用 Integer.parseIntString 转换为 int

It doesn't compile because args, in the main method is an array of Strings. Your methods take an array of ints as argument. You should thus transform the args array of Strings into an array of ints, and pass this new array of ints to your min and max methods. Use Integer.parseInt to transform a String into an int

许一世地老天荒 2024-12-05 14:26:54

看来您在 max() 中有一个拼写错误:

m = nums[j] 应该是 m = args[j]

同样在 min() 中,int mi = nums[0]; 可能应该是 int mi = args[0];

另外,在哪里nums [in main] 来自?您应该先创建此数组,然后再将其传递给 max()min()

it seems you have a typo in max():

m = nums[j] should be m = args[j].

also in min(), int mi = nums[0]; should probably be int mi = args[0];

also, where does nums [in main] comes from? you should create this array before passing it to max() and min()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文