Java 扫描器添加可选的用户输入提示
Java Scanner add optional user input prompts
使用 java util Scanner 将一些参数作为可选用户输入的方法是什么?下面是我的代码。但是,对于所有参数,在输入用户输入之前它会被阻止。
在 'when the user input is entered and then pressed enter key' "OR" 'when just pressed enter key without entering any input'.
的情况下,我希望它继续使用第二个参数
public class MainApplication {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter ordertypes in comma separated format (mandatory): " );
String orderOrActionTypes = in.next();
System.out.println("Enter orderAttributes (optional): " );
String orderAttributes = in.next();
System.out.println("Enter ActionAttributes (optional): " );
String actionAttributes = in.next();
}
}
您仍然可以输入但留空。
当需要输入时,用户可以按 Enter
(结果为空字符串),然后您可以测试用户是否输入了某些内容 orderAttributes.isEmpty()
并且您根据需要执行所需的操作结果。
尝试使用 in.nextLine();
,它可能会有所帮助。
使用 java util Scanner 将一些参数作为可选用户输入的方法是什么?下面是我的代码。但是,对于所有参数,在输入用户输入之前它会被阻止。
在 'when the user input is entered and then pressed enter key' "OR" 'when just pressed enter key without entering any input'.
的情况下,我希望它继续使用第二个参数public class MainApplication {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter ordertypes in comma separated format (mandatory): " );
String orderOrActionTypes = in.next();
System.out.println("Enter orderAttributes (optional): " );
String orderAttributes = in.next();
System.out.println("Enter ActionAttributes (optional): " );
String actionAttributes = in.next();
}
}
您仍然可以输入但留空。
当需要输入时,用户可以按 Enter
(结果为空字符串),然后您可以测试用户是否输入了某些内容 orderAttributes.isEmpty()
并且您根据需要执行所需的操作结果。
尝试使用 in.nextLine();
,它可能会有所帮助。