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

问题描述 投票:0回答:1

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

目前我尝试了一些方法,例如为用户输入的值使用全新的值变量。我很难想象如何向前迈进。

我也尝试使用for循环来制作整个系统,但我发现它没有像预期的那样工作,所以我建议使用.stream代替。

主要课程


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();



    }
}

包含min和max方法的容器类可以从类的底部找到这些方法

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);
    }
}

下一节“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;
    }
}

最终的“Info”类也包含变量

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;

        }

}   

想象一下,如果我发布完整的代码而不是它的片段,每个人都会更容易理解。

谢谢你的帮助!

java arraylist max java-stream min
1个回答
0
投票

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

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);
© www.soinside.com 2019 - 2024. All rights reserved.