英文:
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<Integer> getNumbersInRange(int start, int end) {
List<Integer> result = new ArrayList<>();
for (int i = start; i < 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<Integer> getNumbersInRange(int start, int end) {
List<Integer> result = new ArrayList<>();
for (int i = start; i < end; i++) {
result.add(i);
}
return result;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论