开发者

Maximum and Maximum difference of integers in args variable

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:
      开发者_如何学C      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.


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


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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜