如何在JAVA中使用接受列表作为参数的方法打印范围内的数字?

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

How do I print numbers in range using a method that takes a list as a parameter in JAVA?

问题

这是Mooc Java第3周的第15个练习。以下是练习的要求:

"在练习模板中创建方法public static void printNumbersInRange(ArrayList<Integer> numbers, int lowerLimit, int upperLimit)。该方法打印给定列表中数值在范围[lowerLimit, upperLimit]内的数字。"

我的代码似乎没问题,当我尝试不同的值时,它给出了正确的结果。然而,当我运行测试时,我收到了以下错误消息:

"类PrintInRange的方法printNumbersInRange(ArrayList, int, int)丢失。"

你能帮我解决问题吗?我已经卡在这里一段时间了。以下是我的代码,我只是在主方法中调用了这个方法来进行测试。谢谢!

import java.util.ArrayList;

public class PrintInRange {

    public static void main(String[] args) {
        // 在这里尝试你的方法
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(6);
        list.add(360);
        list.add(-1);
        list.add(10000);

        System.out.println("在范围[300, 100000]内的数字:");
        numbersInRange(list, 300, 100000);
    }

    public static void numbersInRange(ArrayList<Integer> numbers, int lowerLimit, int upperLimit) {
        for (Integer value : numbers) {
            if (value >= lowerLimit && value <= upperLimit) {
                System.out.println(value);
            }
        }
    }
}
英文:

This is week 3 - exercise 15 of Mooc Java. This is what the exercise is looking for

>"Create the method public static void printNumbersInRange(ArrayList&lt;Integer&gt; numbers, int lowerLimit, int upperLimit) in the exercise template. The method prints the numbers in the given list whose values are in the range [lowerLimit, upperLimit]."

My code seems to be fine and it gives me the right results when I try different values. However, when I run the tests I get this error

>"Method printNumbersInRange(ArrayList, int, int) of class PrintInRange missing."

Could you guys give me some light? I have been stuck for a bit here. Follow the code below, I called the method in the main just to test.
Thanks!

import java.util.ArrayList;

public class PrintInRange {

    public static void main(String[] args) {
        // Try your method here
        ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(6);
        list.add(360);
        list.add(-1);
        list.add(10000);
        
        System.out.println(&quot;The numbers in the range[300, 100000]&quot;);
        numbersInRange(list, 300, 100000);
    
       }
    public static void numbersInRange(ArrayList&lt;Integer&gt;numbers, int lowerLimit, int upperLimit){
        for (Integer value : numbers){
            if(value &gt;= lowerLimit &amp;&amp; value &lt;= upperLimit){
                System.out.println(value);
            }
        }
    }
    
}

答案1

得分: 1

以下是代码部分的翻译:

包 com.example;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建于 2020 年 8 月 9 日。
 *
 * 作者:Sergio
 * 自 2020 年 8 月 9 日起
 */
public class PrintRange {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(6);
        list.add(360);
        list.add(-1);
        list.add(10000);

        System.out.println("范围内的数字[300, 100000]");
        numbersInRange(list, 300, 100000);
    }

    static void numbersInRange(List<Integer> numbers, int lowerLimit, int upperLimit) {
        numbers.stream()
                .filter(itemNumber -> itemNumber >= lowerLimit && itemNumber <= upperLimit)
                .forEach(System.out::println);
    }
}

输出部分没有需要翻译的内容。

英文:

The solution is

package com.example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created on 8/9/20.
 *
 * @author sergio
 * @since 8/9/20
 */
public class PrintRange {

    public static void main(String[] args) {

        List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(6);
        list.add(360);
        list.add(-1);
        list.add(10000);

        System.out.println(&quot;The numbers in the range[300, 100000]&quot;);
        numbersInRange(list, 300, 100000);
    }

    static void numbersInRange(List&lt;Integer&gt; numbers, int lowerLimit, int upperLimit) {
        numbers.stream()
                .filter(itemNumber -&gt; itemNumber &gt;= lowerLimit &amp;&amp; itemNumber &lt;= upperLimit)
                .forEach(System.out::println);
    }
}

Output:

The numbers in the range[300, 100000]
360
10000

Process finished with exit code 0

答案2

得分: 1

以下是您要翻译的内容:

package com.company;

import java.util.ArrayList;

public class PrintInRange {

    public static void main(String[] args) {
        // 在这里尝试您的方法
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(6);
        list.add(360);
        list.add(-1);
        list.add(10000);

        System.out.println("在范围[300, 100000]内的数字");
        printNumbersInRange(list, 300, 100000);

    }
    
    public static void printNumbersInRange(ArrayList<Integer> numbers, int lowerLimit, int upperLimit){
        for (Integer value : numbers){
            if(value >= lowerLimit && value <= upperLimit){
                System.out.println(value);
            }
        }
    }

}
英文:

You can try this one

package com.company;


import java.util.ArrayList;

public class PrintInRange {

    public static void main(String[] args) {
        // Try your method here
        ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(6);
        list.add(360);
        list.add(-1);
        list.add(10000);

        System.out.println(&quot;The numbers in the range[300, 100000]&quot;);
        printNumbersInRange(list, 300, 100000);

    }
    public static void printNumbersInRange(ArrayList&lt;Integer&gt;numbers, int lowerLimit, int upperLimit){
        for (Integer value : numbers){
            if(value &gt;= lowerLimit &amp;&amp; value &lt;= upperLimit){
                System.out.println(value);
            }
        }
    }

}

huangapple
  • 本文由 发表于 2020年8月10日 02:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63330117.html
匿名

发表评论

匿名网友

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

确定