如何编写代码(当按回车键时,它将显示下一个)Java



我写的代码如下 -

public class Demo{
public static void main(String[] args){  
System.out.print(“Hearing in the distance”);
System.out.print(“Two mandolins like creatures in the “);  
System.out.print(“dark”);  
System.out.print(“Creating the agony of ecstacy.”);  
System.out.println(“                    - George Barker”);  
}  
} 

我想在按下每个输入按钮后逐个显示每个打印。

`

use java.util.Scanner 如果您调用 Scanner.nextLine((,您的应用程序会等到您编写内容并按 Enter 键,或者您可以按 Enter 键而不写入任何内容。

您可以在下面执行此操作 - 每次按下 Enter 按钮后,它都会打印下一个值,当然您可以相应地编辑代码

private static void printNext(){
Scanner scanner = new Scanner(System.in);
int count  = 3;
while(count> 0) {
if(count == 1){
System.out.println("Hearing in the distance");
}else if(count ==2 ){
System.out.println("Two mandolins like creatures in the");
}else {
System.out.println("Creating the agony of ecstacy.");
}
count--;
if (scanner.hasNextLine()) {
scanner.nextLine();
}
}
}

希望这对您有所帮助。

最新更新