[英]get input Text from JTextField (string to char) to show up in JButton
[英]How do you convert the input from a JTextfield from a string to a char?
对于Hangman,我们有一个方法checkLetter,该方法检查用户输入的字母是否在char []数组中,该数组具有用户试图为Hangman猜测的单词。 这是到目前为止的代码:
public void playGame(String filename)
{
//a randomword is read from a textfile, and converted from a string to an array of chars that has a char stored in each index
randomWord = array[(int)(Math.random() * array.length)];
int length = randomWord.length();
char[] arr = randomWord.toCharArray();
}
然后,我们有了方法checkLetter。
public boolean checkLetter()
{
char inputLetter = letter.charAt(letter.getText()); //letter is the name of the JTextField where the user inputs the letter they are guessing
}
我们对下一步该做什么感到困惑,对于从此处开始的任何帮助,我们将不胜感激。 谢谢!
如果String
的长度为(至少)一,则可以执行以下操作:
String text = letter.getText(); // get the value of the JTextField
char ch = text.charAt(0); // get the first letter as char
或在一份声明中
char ch = letter.getText().charAt(0);
要么
char ch = letter.getText().toCharArray()[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.