【发布时间】:2019-09-05 18:20:18
【问题描述】:
所以基本上我试图从 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;
}
}
认为如果我发布完整代码而不是 sn-ps,每个人都会更容易理解。
感谢您的帮助!
【问题讨论】:
-
您的代码看起来不错,除了一些编码约定问题。你到底想要什么??
-
您刚刚注释了几行代码。它怎么说你想要什么??
-
@raviiii1 PrintSmaller 和 PrintLarger 部件需要有用户输入的值,而不是我它有像 30 这样的“锁定值”。目前它将 arraylist 的值与“30”进行比较,并选择更小或更大的值.我想要做的是将“30”更改为用户可以选择的值,例如用户可以输入 56,它会打印小于或大于该值的值。
-
你知道如何使用参数化函数吗?使用此链接寻求帮助cs.toronto.edu/~reid/web/javaparams.html
标签: java arraylist max java-stream min