我有下面这个java程序,它在没有while循环的情况下工作正常,但我想运行执行,直到用户从键盘按下Q键。Java:按下“Q”键后终止while循环
那么,什么样的条件应该放在while循环打破循环?
import java.awt.event.KeyEvent;
import java.util.Scanner;
import static javafx.scene.input.KeyCode.Q;
public class BinaryToDecimal {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(kbhit() != Q){
System.out.print("Input first binary number: ");
try{
String n = in.nextLine();
System.out.println(Integer.parseInt(n,2));
}
catch(Exception e){
System.out.println("Not a binary number");
}
}
}
}
任何帮助将是伟大的。 谢谢。
我知道的kbhit()在C语言中,但不知道在java中 –
问题不清楚,直到你证明方法 –
那么究竟什么是你的问题?你想知道,你如何阅读键盘输入? – dunni