如果按下Esc,则打破循环 [英] break a loop if Esc was pressed

查看:111
本文介绍了如果按下Esc,则打破循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用JAVA语言编写了一个程序,它使用Scanner类接受来自控制台的输入....

I have written a program in JAVA language which accepts inputs from console by using Scanner class....

现在我想将此功能添加到我的代码中当用户按下Esc Button时,存在一个循环(while)。
到目前为止我认为Keyboard类可以帮助我,但它就像Scanner ...我试图使用事件但不知道如何正确使用它们....

now I want to add this ability to my code to exist a loop (while) when user presses Esc Button. so far I thought Keyboard class can help me but it was just like Scanner...I tried to use events but do not know how to use them correctly....

源代码:

    package switchCase_v1;

     import cs1.Keyboard;
     import java.util.EventObject;
     import java.awt.AWTEvent;
     import java.awt.event.KeyEvent;
     import java.awt.event.ComponentEvent;
     import java.awt.event.InputEvent;
     import java.util.*;

      public class SwithCase {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("enter the name or number of month: ");
        int monthNumber = input.nextInt();

        while (true) {
            KeyEvent button;
            if (button.getKeyCode() == 27)
                break;
            else if (monthNumber == '\n') {
                System.out.println("enter a number");
                monthNumber = input.nextInt();
            } else {
                switch (monthNumber) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    System.out.println("it has 31 days");
                    monthNumber = input.nextInt();
                    break;
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                    System.out.println("it has 30 days");
                    monthNumber = input.nextInt();
                    break;
                default:
                    System.out.println("it is not a valid number");
                    monthNumber = input.nextInt();
                    break;
                }
            }

        }
    }
  }

当我想考虑Esc或Enter等点击按钮时,我如何处理案件?我认为它也适用于使用ASCII码。

How I can deal with cases when I want to take into consideration the hitting buttons like "Esc" or "Enter"? I think it should be also applicable by using ASCII codes.

这是我的代码的新版本:

this is the new version of my code:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    System.out.print("Check number of days");
    KeyEvent e;
    if (e.getKeyCode() == KeyEvent.VK_ENTER)
    {
    System.out.println("enter the name or number of month: ");
    int monthNumber=input.nextInt();
    }
    else if (Keyboard.getEventKey()==Keyboard.KEY_ESCAPE)
    {
        System.out.println("GoodBye");
    }
    }   

}

但它有一个错误,说e对象可能没有被初始化... !!!!!我该怎么办?!!!

but it has an error saying e object may not have been initialized...!!!!!what should I do?!!!

推荐答案

您目前正在创建一个命令行应用程序,它从标准输入读取内容并将内容打印到标准输出。处理按钮的方式完全取决于运行程序的终端,大多数终端在按下转义时不会向应用程序的stdin发送任何内容。

You're currently making a command-line application which reads stuff from standard input and prints stuff to standard output. How buttons presses are handled depends entirely on the terminal in which your are running your program, and most terminals won't send anything to your application's stdin when escape is pressed.

如果要捕获关键事件,则必须使用AWT或Swing创建GUI应用程序。如果您只想在程序运行时终止程序,请尝试按Ctrl + C(这适用于大多数终端)。

If you want to catch key events, you'll have to make a GUI application using AWT or Swing. If all you want is to terminate your program while it's running, try pressing Ctrl+C (this works in most terminals).

这篇关于如果按下Esc,则打破循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆