gpt4 book ai didi

Java-Android 如何将字符串中的字母转换为指定数字

转载 作者:行者123 更新时间:2023-12-01 11:42:19 24 4
gpt4 key购买 nike

程序详细说明:该程序接受输入字符串(字母、单词等),并将所有字母转换为为每个字母字符分配的数值,然后将结果存储在数组中。

字母字符的数字分配示例如下所示:

1 = A I J Q Y
2 = B K R
3 = C G L S
4 = D M T
5 = E H N X
6 = U V W
7 = O Z
8 = F P

这是我在 Android 上使用 Java 的代码示例:

public class NameCon extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nameconvert);

Button btn = (Button) findViewById(R.id.btnConvert);
Button clear = (Button) findViewById(R.id.btnClearName);
final EditText et1 = (EditText) findViewById(R.id.edittxtName);
final TextView result = (TextView) findViewById(R.id.txtViewSum);

result.setText("Input Character Only");
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Map<Character, Integer> map;
String str = new String(et1.getText().toString());
str = str.replaceAll("","");
char[]crt = str.trim().toCharArray();
int[] intArray = new int[crt.length];

map = new HashMap<>(26);
map.put('a', 1);
map.put('i', 1);
map.put('j', 1);
map.put('q', 1);
map.put('y', 1);
map.put('b', 2);
map.put('k', 2);
map.put('r', 2);
map.put('c', 3);
map.put('g', 3);
map.put('l', 3);
map.put('s', 3);
map.put('d', 4);
map.put('m', 4);
map.put('t', 4);
map.put('e', 5);
map.put('h', 5);
map.put('n', 5);
map.put('x', 5);
map.put('u', 6);
map.put('v', 6);
map.put('w', 6);
map.put('o', 7);
map.put('z', 6);
map.put('f', 8);
map.put('p', 8);

for(int i = 0; i < crt.length; i ++){
intArray[i] = map.get(crt[i]);
result.setText("Values is "+ intArray[i]);
}
}
});
}
}

我现在的问题是我无法得到如下所示的结果。然后程序将字母转换为数字。然后对每个字母求和。

这是我想展示的: 使用键盘输入姓名 => Tim Cahill
输出显示在 TextView 上 = 25

非常感谢您对我的帮助。

最佳答案

这应该有效:

public class NameCon extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nameconvert);

Button btn = (Button) findViewById(R.id.btnConvert);
Button clear = (Button) findViewById(R.id.btnClearName);
final EditText et1 = (EditText) findViewById(R.id.edittxtName);
final TextView result = (TextView) findViewById(R.id.txtViewSum);
Map<Character, Integer> map = new HashMap<>(26);
map.put('a', 1);
map.put('i', 1);
map.put('j', 1);
map.put('q', 1);
map.put('y', 1);
map.put('b', 2);
map.put('k', 2);
map.put('r', 2);
map.put('c', 3);
map.put('g', 3);
map.put('l', 3);
map.put('s', 3);
map.put('d', 4);
map.put('m', 4);
map.put('t', 4);
map.put('e', 5);
map.put('h', 5);
map.put('n', 5);
map.put('x', 5);
map.put('u', 6);
map.put('v', 6);
map.put('w', 6);
map.put('o', 7);
map.put('z', 6);
map.put('f', 8);
map.put('p', 8);

result.setText("Input Character Only");
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String str = et1.getText().toString();
char[]crt = str.trim().toCharArray();
int sum = 0;

for(int i = 0; i < crt.length; i ++){
sum += map.get(crt[i]);
}
result.setText("Values is "+ sum);
}
});}}

希望对你有帮助!

关于Java-Android 如何将字符串中的字母转换为指定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com