Java的:如何从键入某个字符停止控制台输入 [英] Java: How to stop console input from typing a certain character

查看:912
本文介绍了Java的:如何从键入某个字符停止控制台输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做刽子手程序,我想为字母/字母/符号一吨的布尔值,以及他们会做的是,当他们是真的,我希望控制台不允许的,该分型性格。

I'm making a hangman program, and I want to have a ton of Booleans for the alphabet / letters / symbols, and what they would do is, when they are true, I want the console to DISALLOW the typing of that character.

例如:我已经输入,将AS字符和pressed输入。我的程序现在有G = True布尔。我需要system.forceConsole.input.stop(forLetter:g)或一些爵士乐一样,所以它甚至不能在控制台中再次键入克选项

For example: I have typed g as a character and pressed Enter. My program now has the g=true boolean. I need system.forceConsole.input.stop(forLetter:g) or some jazz like that so it CANNOT even be an option to type g in console again.

我只是想知道这是否甚至远程可能的,如果它是...我能看到剪切/粘贴例子,看看它是什么我要找的?

I only want to know if this is even remotely possible, and if it is... could I see a cut/paste example to see if it what I'm looking for?

下面是只是一个小例子code我在想什么的,绝不会其实这工作,但它应该表现出什么我要为...

Below is just a little example code of what I'm thinking of, by no means will this actually work, but it should show what I'm going for...

      input = new Scanner(System.in);
      boolean a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
      //set all listed booleans to false
      char userCharacter;


      System.out.print("Please enter a letter");
      userCharacter=input.nextChar();
      if (userCharacter==letterArray){//That would be an array to get the letters 
                                      //Still don't know how to use them
      if (letterArray == true)
      {System.deny.Console.input(forChar:letterArray)}
      }

我看想看看它甚至在其他一些系统code存在的最重要的事情就是System.deny.Console.input的事情,我不担心没有工作的计划,因为这只是榜样code这并不意味着工作 - 但要获得跨越点。所以,你不必批评我的code的样子,因为这是相当困难的,使网站上的code的斑点,所以我真的不希望把它真正阐述。

The biggest thing I'm looking at trying to see if it even exists in some other System code is the "System.deny.Console.input" thing, I'm not worried about the program not working because, this is just "example" code that was not meant to work - but to get a point across. So you don't have to critique me on the way the code looks, because it is rather difficult to make the "code" blotches on the site so I didn't really want to make it really elaborate.

推荐答案

我建议类似如下:

public class Hangman {
  boolean usedLetters[] = new boolean[26];
  boolean gameOver = true;

  String inputString = "";
  char testLetter = 0;

  public static void main(String[] args) {
    // Initialize each boolean in the array to 'false'.
    Arrays.fill(usedLetters, false);

    System.out.println("Try a letter by typing it and pressing enter");
    System.out.println("(additional letters will be ignored, lower case only)");


    while (!gameOver) {
      // Get the characters entered on the console before enter is pressed
      inputString = System.console.readLine();

      // Get the first letter entered and adjust the value so that a = 0, b = 1, etc...
      testLetter = inputString.toCharArray()[0] - 'a';

      // Check to see if the letter is valid
      if (testLetter <= 25 && testLetter >= 0) {
        // Check if letter has beed tried before
        if (!usedLetters[testLetter]) {
          // Do something to check the letter
          tryLetter(testLetter);
          // Remember that the letter has been tried
          usedLetters[testLetter] = true;
        } else {
          // Do nothing
        }
      }
    }
  }

  public static tryLetter(char letter) {
    // Insert your code to update the display here.
    // Remember to set 'gameOver' to 'true' when you want to stop accepting input.
  }
}

尽管这是一个完整的类,它不会真的对自己有用的东西,因为没有实际的游戏逻辑在里面。

Although this is a complete class, it won't really do anything useful on its own, since there is no actual game logic in there.

这篇关于Java的:如何从键入某个字符停止控制台输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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