增加一个char值 [英] Increase a char value by one

查看:147
本文介绍了增加一个char值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 EditText 的应用程序和一个名为forward的按钮。所有我想要的只是当我在 EditText 中键入a,然后单击按钮时,将在 EditText ,再次按下c等等。我试过:

I have an app with an EditText and a button named "forward". All I want is just when I type "a" in the EditText and click the button, the word "b" to be typed in the EditText, "c" if pressed again and so on. I have tried:

value = edt.getText();
edt.setText(value + 1);

但是当然会打印初始字符串后跟数字1。有任何想法吗?感谢很多

But that of course will print the initial string followed by the number "1". Any ideas? Thanks a lot

推荐答案

测试和工作

所有信件都可以通过其 ASCII 值来表示。

All letters can be represented by their ASCII values.

如果您将这些信件投给一个 int ,添加1,然后转回一个 char ,该字母将增加1个ASCII值(下一个字母)。

If you cast the letters to an int, add 1, and then cast back to a char, the letter will increase by 1 ASCII value (the next letter).

例如:

'a' 97

'b' 98

所以如果输入是'a',你把它转换成一个 int ,您将获得 97 。然后添加1并获得 98 ,然后再次将其重新转回 char 并获取'b'

So if the input was 'a' and you casted that to an int, you would get 97. Then add 1 and get 98, and then finally cast it back to a char again and get 'b'.

以下是一个投射示例:

System.out.println( (int)('a') ); // 97
System.out.println( (int)('b') ); // 98
System.out.println( (char)(97) ); // a
System.out.println( (char)(98) ); // b






所以,你的最终代码可能是这个:


So, your final code might be this:

// get first char in the input string
char value = et.getText().toString().charAt(0);

int nextValue = (int)value + 1; // find the int value plus 1

char c = (char)nextValue; // convert that to back to a char

et.setText( String.valueOf(c) ); // print the char as a string

当然,只有有一个字符作为输入。

Of course this will only work properly if there is one single character as an input.

这篇关于增加一个char值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆