简体   繁体   English

我的方法怎么了?

[英]What's wrong with my method?

I'm completely new to Java, so forgive me for being an idiot about some of this. 我是Java的新手,所以请原谅我是其中的一部分。 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. 我应该写一个静态方法isStrictlyIncreasing(double[] in) ,如果给定数组中的每个值都大于它之前的值,则返回true,否则返回false。 Also, I can't use a java.util.ArrayList . 另外,我不能使用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" 不幸的是,我一直得到“ true,true,true ...答案:false”的列表。

I know there is something wrong with my method, possibly in the if-statement and was wondering if someone could help me please. 我知道我的方法有问题,可能是在if语句中,并且想知道是否有人可以帮助我。

Firstly, in Java it's common practice to camelCase method names. 首先,在Java中,通常使用camelCase方法名称。 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. 其次,return语句用于从方法返回,因此您可能不想在每次检查后返回。 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. 如果序列中的下一个数字不大于之前的值,则将返回false。 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 然后,如果它通过for循环而不被触发,那么我们知道它们必须按升序排列,因此返回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. 不满足条件时,您将立即返回false。 Else it will return true. 否则它将返回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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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