Scanner sc = new Scanner(System.in);
String input = null;
do {
System.out.println("Type X to exit and hit enter");
input = sc.next();
} while(!input.equalsIgnoreCase("x"));
sc.close();
第二个例子:
Scanner sc = new Scanner(System.in);
String input = null;
do {
System.out.println("Type X to exit and hit enter");
input = sc.next();
if (input.equalsIgnoreCase("x")) {
break;
}
} while(true);
sc.close();
Scanner sc = new Scanner(System.in);
//Type in "a" to end
while (!sc.next().equalsIgnoreCase("a")) {
// console code here
}
System.out.println("Exiting");
2条答案
按热度按时间7qhs6swi1#
两个做你想做的事情的例子:
第一个例子:
第二个例子:
lymnna712#
将控制台代码添加到while-loop;按“a”或“A”退出。