按下某个键时如何执行代码块?

时间:2018-06-01 14:31:21

标签: java class input keyboard invoke

所以基本上我试图找到一种方法来执行一个代码块,当我按下一个键而不是必须扫描一个字符串然后按Enter来调用它。这是我第一次在这里问一个问题,在谈到代码时,我并没有真正前进。只是想学习能够提升我编码知识的新东西。这是我目前的代码:

主要方法(我试图改变我在变量中扫描的部分"键"):

import java.util.Scanner;

public class testGame {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    dungeon numberOne = new dungeon();

    String key;
    numberOne.setPlayer();
    numberOne.printLayout();

    Scanner input = new Scanner(System.in);
    for(int x = 0; x < Integer.MAX_VALUE; x++)
    {
        key = input.next();
        if(key.equals("w"))
        {
            numberOne.moveUp();
            numberOne.printLayout();

        }
        else if(key.equals("d"))
        {
            numberOne.moveRight();
            numberOne.printLayout();

        }
        else if(key.equals("s"))
        {
            numberOne.moveDown();
            numberOne.printLayout();

        }
        else if(key.equals("a"))
        {
            numberOne.moveLeft();
            numberOne.printLayout();

        }
        else
        {
            numberOne.printLayout();
        }
    }
}
}

助手班级:

public class dungeon {
    private monster[] randMonster = new monster [10];
    private String printDungeon ="";
    private int x = 17;
    private int y = 34;
    private String pastLayout = "H";
    private String currentPostion = "O";
    private String[][] layout = //I deleted this as it was long. It was an 
 ASCII art layout..


public void printLayout()
    {
        for(int x = 0; x < layout.length; x ++)
        {
            for(int y = 0; y < layout[0].length; y++)
            {
                printDungeon = printDungeon + layout[x][y];
            }
            System.out.println(printDungeon);
            printDungeon = "";
        }
    }
    public void setPlayer()
    {
        layout[x][y] = "O";
    }
    public void moveUp()
    {
        if(layout[x-1][y].equals("#"))
        {}
        else
        {
        layout[x][y]=pastLayout;
        pastLayout = layout[x-1][y];
        layout[x-1][y] = currentPostion;
        x--;
        }
    }
    public void moveDown()
    {
        if(layout[x+1][y].equals("#"))
        {}
        else
        {
        layout[x][y]=pastLayout;
        pastLayout = layout[x+1][y];
        layout[x+1][y] = currentPostion;
        x++;
        }
    }
    public void moveLeft()
    {
        if(layout[x][y-1].equals("#"))
        {}
        else
        {
        layout[x][y]=pastLayout;
        pastLayout = layout[x][y-1];
        layout[x][y-1] = currentPostion;
        y--;
        }
    }
    public void moveRight()
    {
        if(layout[x][y+1].equals("#"))
        {}
        else
        {
        layout[x][y]=pastLayout;
        pastLayout = layout[x][y+1];
        layout[x][y+1] = currentPostion;
        y++;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

编辑:这假设您使用javafx制作GUI。我不确定它是否在命令行中有效,但可能不是:/

编辑2:一件小事,但如果你在每个案例后更新布局,你只需更新if / else范围之外的布局。优良作法是分解代码并避免复制粘贴编码

使用EventHandlers和KeyEvents有一种非常简单的方法!

这是一个使用switch语句的实现。你可以用if / else块代替交换机,如果你觉得更适合你,但是如果你不知道switch语句,并且想要提高你对编码的了解,那就一定要看看它们!

import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class SomeClass {
    public SomeClass() {
        //constructor elided
    }
    //Within your class containing your methods, have this private class.
    private class KeyHandler implements EventHandler<KeyEvent> {
        @Override
        public void handle(KeyEvent e) {
            KeyCode keyPressed = e.getCode();

            switch (keyPressed) {

            case W:
                //do W thing
                break;
            case A:
                //do A thing
                break;
            case S:
                //do S thing
                break;
            case D:
                //do D thing
                break;
            default:
                break;
            }
        }
    }
}

祝你好运!玩得开心! 附:您可以取出“// TODO自动生成的方法存根”注释 - 这就是编辑软件中的注释,该方法需要填写!