或者显示在文本框中键入任何文本如何小写字母转换为大写字母和大写字母为小写字母
// 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