有没有办法读取控制台中的上一行?我当前的代码打印出 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");
}
最佳答案
正如 ELLiot 提到的,您无法读回它,但您可以通过将数据存储在某个变量中来跟踪它,如下所示:
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");
}
关于java - 我们如何读取控制台中的上一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033945/