Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a Java program that automates a long list of tasks and it pauses for 15 seconds after each completed task then restarts. Is there a way to end this program by entering any keystrokes from the user? I don't want to have to ask the user if they want to end the program and have to stop and ask every so often as the user is not going to be around to respond. I know you can press "Control + C" to stop the program while in the command prompt but is there a more elegant way than this? The program is something like this:

while(true) {
// automatic task code
}

Thank You

**Edit* My question is more of a "Can I make a program when I input an unsolicited input that can abruptly end the program?"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
287 views
Welcome To Ask or Share your Answers For Others

1 Answer

A simple break would do. Calling break exits the loop.

while(true) {

  if(conditionMet){ 
      break;
  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...