![](/img/trans.png)
[英]I'm trying to read the previous line of output in the console and save it to an arraylist that I can call upon but everything is seperated by ifs
[英]How can we read the previous line in console?
有没有办法在控制台中读取上一行? 我当前的代码会打印出“ Hello”,如果包含或不包含单词,我想打印另一条消息?
我在下面写了一个例子。 我知道if语句中需要代码,但是我无法确定需要什么代码。
System.out.println("Hello");
if (something){
System.out.println("Line above contains the word Hello");
}
else(!something){
System.out.println("Line above does not contain the word Hello");
}
正如ELLiott所提到的,您不能读回它,但是可以通过将数据存储在某些变量中来进行跟踪,如下所示:
String line="Hello";
System.out.println(line);
if (line.contains("Hello")){
System.out.println("Line above contains the word Hello");
}
else{
System.out.println("Line above does not contain the word Hello");
}
您无法读回在控制台上打印的内容,但始终可以通过将其存储在变量中来保持跟踪。
这是一种实现方法:
String something = "Hello";
System.out.println(something);
if (something.contains("Hello")) {
System.out.println("Line above contains the word Hello");
} else {
System.out.println("Line above does not contain the word Hello");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.