或者顯示在文本框中鍵入任何文本如何小寫字母轉換爲大寫字母和大寫字母爲小寫字母
// in either Capital or lowercase depending on the original
// letter changed. For example: CoMpUtEr will convert to
// cOmPuTeR and vice versa.
Switch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
String characters = (SecondTextField.getText()); //String to read the user input
int length = characters.length(); //change the string characters to length
for(int i = 0; i < length; i++) //to check the characters of string..
{
char character = characters.charAt(i);
if(Character.isUpperCase(character))
{
SecondTextField.setText("" + characters.toLowerCase());
}
else if(Character.isLowerCase(character))
{
SecondTextField.setText("" + characters.toUpperCase()); //problem is here, how can i track the character which i already change above, means lowerCase**
}
}}
});
你有一個'else',所以如果它開始爲大寫它不會經過'if'的第二個分支機構。如果它以小寫開始,它將通過第二個分支(而不是第一個分支) - 你實際上有什麼問題? – John3136 2013-02-20 03:59:28
同意。看起來它應該已經工作了 – 2013-02-20 04:00:21
哦,我看到了問題..不要設置characters.toUpperCase()或類似。一次只更改一個角色。你的循環已經被設置爲這樣做,但你正在改變整個字符串的情況。 – 2013-02-20 04:01:35