我有下面這個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