方法生成数字列表,在JAVA中返回空数组。

huangapple go评论91阅读模式
英文:

method to generate list of numbers returns empty array in JAVA

问题

以下是您要翻译的代码部分:

public class Bonus {
    public static void main(String[] args) {
        printDatesBetween(2018, 2015);
    }

    public static void printDatesBetween(int fromYear, int toYear){
         System.out.print(getNumbersInRange(fromYear, toYear));
    }

    public static List<Integer> getNumbersInRange(int start, int end) {
        List<Integer> result = new ArrayList<>();
        for (int i = start; i < end; i++) {
            result.add(i);
        }
        return result;
    }   
}

如果您需要进一步的帮助,请随时告诉我。

英文:

Just beginning to play with Java and not sure how to search this problem:
I want to generate an array of numbers in the given range with a method getNumbersInRange,

but when I call this method and pass the arguments, it returns empty array:

public class Bonus {
	public static void main(String[] args) {
		printDatesBetween(2018, 2015);
	}
	
	public static void printDatesBetween(int fromYear, int toYear){
	     System.out.print(getNumbersInRange(fromYear, toYear));
	}
	
	public static List&lt;Integer&gt; getNumbersInRange(int start, int end) {
	    List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
	    for (int i = start; i &lt; end; i++) {
	        result.add(i);
	    }
	    return result;
	}	
}


</details>


# 答案1
**得分**: 0

你正在按降序传递值并创建循环。
你应该像这样做。

```java
public class Bonus {
    public static void main(String[] args) {
        printDatesBetween(2015, 2018);
    }
    
    public static void printDatesBetween(int fromYear, int toYear){
         System.out.print(getNumbersInRange(fromYear, toYear));
    }
    
    public static List<Integer> getNumbersInRange(int start, int end) {
        List<Integer> result = new ArrayList<>();
        for (int i = start; i < end; i++) {
            result.add(i);
        }
        return result;
    }   
}
英文:

you are passing values in descending order and creating loop.
you should do something like this.

public class Bonus {
public static void main(String[] args) {
    printDatesBetween(2015, 2018);
}

public static void printDatesBetween(int fromYear, int toYear){
     System.out.print(getNumbersInRange(fromYear, toYear));
}

public static List&lt;Integer&gt; getNumbersInRange(int start, int end) {
    List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
    for (int i = start; i &lt; end; i++) {
        result.add(i);
    }
    return result;
}   

}

huangapple
  • 本文由 发表于 2020年9月27日 23:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64090407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定