![](/img/trans.png)
[英]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.