繁体   English   中英

我们如何在控制台中读取上一行?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM