如何使用用户输入的值 "X" 从 arraylist 打印所有小于或大于 "X" 值的值

How to use user inputted value "X" to print all values smaller or larger than "X" value from arraylist

所以基本上我试图打印 arraylist 中小于或大于用户输入值的所有值。 例如,如果我输入“500”,它将打印所有小于该值的值,然后我可以对更大的值执行相同的操作。 我该如何前进?目前它使用流来打印小于或大于 "30"

的值

目前我已经尝试了几种方法,比如对用户输入的值使用全新的值变量。我很难想象如何推进这个。

我也曾尝试使用 for 循环来构建整个系统,但我发现它并没有像预期的那样工作,所以我被推荐使用 .stream。

主要class


import java.util.Scanner;

public class MainClass {

        public static void main( String [] args ) {
            Scanner sc = new Scanner( System.in );
            Container list = new Container();

        System.out.println("Enter amount (1-5)");
        int count = sc.nextInt();
        sc.nextLine();

        for (int i = 0; i < count; i++) { 
            System.out.println("Enter Type"); 
            String type = sc.nextLine();

            System.out.println("Enter Type2");
            String type2 = sc.nextLine();   

            System.out.println("Enter value");
            int value = sc.nextInt();
            sc.nextLine();

            MoreInfo moreInfo = new MoreInfo(type, type2);
            Info info = new Info(moreInfo, value);
            list.Add(info);     


        }

        list.PrintAll();
        System.out.println("Smaller than 30");
        list.PrintSmaller();
        System.out.println("Larger than 30");
        list.PrintLarger();



    }
}

确实包含最小值和最大值方法的容器 class 这些方法可以在 class

的底部找到
import java.util.ArrayList;
import java.util.Iterator;

public class Container {
    private MoreInfo moreInfo;
    public ArrayList <Info> InfoArray;

    public Container() {
        InfoArray = new ArrayList <Info>();
        this.setMoreInfo(moreInfo);
    }


    public MoreInfo getMoreInfo() {
        return moreInfo;
    }


    public void setMoreInfo(MoreInfo moreInfo) {
        this.moreInfo = moreInfo;
    }


    public boolean Add(Info addInfo) {
        return InfoArray.add(addInfo);
    }




public void PrintAll() {

        Iterator<Info> iter = InfoArray.iterator();

        while(iter.hasNext()){
            Info info = iter.next();

            System.out.println( info.getValue() + info.getMoreInfo().getType2() + info.getMoreInfo().getType());

        }
    }


public void PrintSmaller() {

    InfoArray.stream().filter(e -> e.getValue() < 30()).forEach(System.out::println);

    }

public void PrintLarger() {

    InfoArray.stream().filter(e -> e.getValue() > 30()).forEach(System.out::println);
    }
}

下一个class"MoreInfo"包含变量

package test;

public class MoreInfo {

    private String type;
    private String type2;

    public MoreInfo(String Type, String Type2) {
        this.type2 = Type2;
        this.type = Type;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType2() {
        return type2;
    }

    public void setType2(String Type2) {
        this.type2 = type2;
    }

    public String toString(){
        return type + " " + type + " " + type2;
    }
}

Final "Info" class 也包含变量

package test;



public class Info {

        private MoreInfo moreInfo;
        private int value;

        public Info(MoreInfo moreInfo, int value) {
            this.moreInfo = moreInfo;
            this.value = value;
        }

        public MoreInfo getMoreInfo() {
            return moreInfo;
        }

        public int getValue() {
            return value;
        }
        public String toString(){
            return moreInfo + " " + value;

        }

}   

我想如果我 post 完整的代码而不是它的片段会更容易让每个人理解。

感谢您的帮助!

将你想要找到较小数字的数字存储到一个变量中,比如说

int arg;
Scanner sc = new Scanner(System.in)
arg = sc.nextInt()

修改打印较小和打印较大的函数以具有 1 个参数,就像这样...

public void PrintSmaller(int number) {

    InfoArray.stream().filter(e -> e.getValue() < number()).forEach(System.out::println);

    }

public void PrintLarger(int number) {

    InfoArray.stream().filter(e -> e.getValue() > number()).forEach(System.out::println);
    }
}

将 arg 传递到方法调用中...

System.out.println("Smaller than " + arg);
list.PrintSmaller(arg);
System.out.println("Larger than " + arg);
list.PrintLarger(arg);