从用户控制台获取输入 - Java 代码[重复]

问题描述 投票:0回答:4

此 C++ 代码片段的 Java 等效项是什么:

字符串 str ; while(cin>>str){ //代码 }
java c++ console-application user-input
4个回答
2
投票

类似这样的事情

String str ; 
while((str = System.in.readLine()) != null){
   //code
}

0
投票
Scanner scanner = new Scanner(System.in);
String sentence = scanner.nextLine();

0
投票
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while(flag)
{
   String str = scanner.nextLine();

   // Add any condition to set flag = false to exit from while loop
}

-1
投票

您可以使用 GUI 风格来完成。

import javax.swing.JOptionPane;
String Input=JOptionPane.showInputDialog("Title","Message");
© www.soinside.com 2019 - 2024. All rights reserved.