[英]break program when user enters specific string into input
我想让用户输入一些字符串,程序接受控制台输入,直到用户输入“/done”..所以它是如何工作的:
打印给用户:输入您的字符串
用户输入:你好日食。
嗨测试等等等等
bla 456 testmore /done
只要用户在任何大小的任何字符串中输入 /done,程序就会中断。 如果您按“回车”键,程序将不会结束。 它只会在您输入 /done 时结束.. 到目前为止我是如何设置我的程序的:
Scanner 123 = new Scanner(System.in);
string input = "";
System.out.println("Enter your string: ");
do {
input = 123.nextLine();
System.out.print("Rest of program here..");
}
while (!input.equals("/done"));
我尝试在 while 循环下放置如下所示的内容,但我认为我做得不对。
while (!input.equals("/done"));
if input.equals("/done");
break;
}
我知道使用 do-while 循环,只要 while 中的布尔值是假的,它就会继续。 所以对于我的程序,程序接受输入直到用户输入 /done 所以布尔值是假的,直到输入字符串 /done 。 然后根据上面的逻辑,一旦输入等于“/完成”,程序就会中断
关于我做错了什么的任何想法?
Scanner s=new Scanner(System.in);
String input = "";
String[] parts;
System.out.println("Enter your string: ");
while (true){
input=s.nextLine();
if (input.contains("/done")){
parts=input.split("/done");
//Use parts[0] to do any processing you want with the part of the string entered before /done in that line
break;
}
}
我认为这会正常工作
Scanner scanner = new Scanner(System.in);
String input = "";
System.out.println("Enter your string: ");
do {
input = scanner.nextLine();
} while (!input.contains("/done"));
System.out.print("Rest of program here..");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.