【问题标题】:Would this array work to check letters entered by user?这个数组可以检查用户输入的字母吗?
【发布时间】:2011-09-06 10:57:29
【问题描述】:

这是否可以作为一个数组来检查程序用户输入的字母?

final int LOW = 'A';  //smallest possible value
final int HIGH = 'Z';  //highest possible value

int[] letterCounts = new int[HIGH - LOW + 1];  
String guessletter;
char[] guessletter;
int offset;  //array index
// set constants for the secret word and also "!" to guess the full word
final String GUESS_FULL_WORD = "!";
final String SECRET_WORD = "APPLE";
// set integer value for number of letters for the length of the secret word
// set integer value for the number of guesses the user have made. starting at zero.
int numberofletters, numberofguesses;
numberofguesses = 0;

// guessletter indicates the letter that the user is guessing
// guessword indicates the word that the user is guessing after typing "!"
// new screen indicates the change made to the screen
// screen is the game screen that contains all the "_"'s
String guessletter, guessword, newscreen;
String screen = "";
numberofletters = SECRET_WORD.length ();

/* prompt user for a word */
c.print("Enter a letter: ");
guessletter = c.readLine();

【问题讨论】:

  • 您的代码不完整(没有任何用处)。
  • 另外,它不能编译,因为你有三个(!)guessletter 声明...

标签: java variable-assignment


【解决方案1】:

正如@Bernard 指出的那样,您粘贴的代码除了向用户询问一个字母外,没有任何作用。如果您的问题是您是否在跟踪用户猜到的字母方面是否走在正确的轨道上(这样程序就不会接受已经猜到的字母),那么是的,您可以使用letterCounts为此,通过增加与用户猜测的字母相对应的数组元素。如果你卡在某件事上,你应该更清楚问题是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多