简体   繁体   中英

What's wrong with my method?

I'm completely new to Java, so forgive me for being an idiot about some of this. I'm supposed to write a static method isStrictlyIncreasing(double[] in) that returns true if each value in the given array is greater than the value before it, or false otherwise. Also, I can't use a java.util.ArrayList .

Here's my code:

public static void main(String[] args) {

    double in[] = new double[]{45, 15, 25, 79, 89, 45, 66, 33, 56, 105};
    Sort(in);
    System.out.println("Answer: " + Sort(in));
}

private static boolean Sort(double[] in) {
    int n = in.length;
    int temp = 0;    
    for(int i = 0; i < n; i++){
        for(int j = 1; j < (n-i); j++){
            if(in[j - 1] < in[j]){
                return true;
            }
            return false;
        }
    }

Unfortunately, I just keep getting a list of "true, true, true...Answer: false"

I know there is something wrong with my method, possibly in the if-statement and was wondering if someone could help me please.

Firstly, in Java it's common practice to camelCase method names. That is,

private static boolean Sort(double[] in) {

should become

private static boolean sort(double[] in) {

Secondly, return statements are used to return from a method, so you likely won't want to return after each check. Rather, you would want to do something like so,

private static boolean Sort(double[] in) {
    int n = in.length;
    int temp = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {
            if (in[j - 1] > in[j]) {
                return false;
            }
        }
    }
    return true;
}

What this will do is return false if the next number in the sequence is NOT greater than the value before it. And then if it makes it through the for loops without being triggered, then we know that they must be in ascending order, hence return true

Don't you just need to check only the value just before ? You'll return false as soon as your condition is not met. Else it will return true.

   public static void main(String[] args) {

        double in[] = new double[]{45, 15, 25, 79, 89, 45, 66, 33, 56, 105};
        Sort(in);
        System.out.println("Answer: " + Sort(in));
    }

    private static boolean Sort(double[] in) {
        int n = in.length;
        int temp = 0;    
        for(int i = 1; i < n; i++){
           if(in[i - 1] < in[i])
              return false;
        }
        return true;
  }

you dont need a double loop since you are only checking for consecutive values.

private static boolean Sort(double[] in) {
        int n = in.length;

        for(int i = 1; i < n-1; i++){
                if(in[i - 1] < in[i]){
                    return true;
                }

            }
        return false;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM