有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:按“Q”键后终止while循环

我有下面这个java程序,它在没有while循环的情况下运行良好,但我希望在用户按下键盘上的Q键之前运行该程序

那么,当循环中断时,应该在循环中设置什么条件呢

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");
            }
        }
    }
}

任何帮助都会很好。 谢谢


共 (0) 个答案