gpt4 book ai didi

java - 数字如何读/拼写为字母?前 : 213 as BAC. (jsp)

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:48 25 4
gpt4 key购买 nike

我有数字,它们是 213。我想把它加密成字母表,213加密成BAC。但是当我运行代码时它没有显示任何内容。

这里是代码。

Calculate.java//java servlet 文件

         String str = Integer.toString(numbers); 
//it because numbers in int type, then i convert to string type first before spelled
char[] alp;
alp = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i)=='1'){
alp[i]='a';
}else if(str.charAt(i)=='2'){
alp[i]='b';
}
else if(str.charAt(i)=='3'){
alp[i]='c';
}
else if(str.charAt(i)=='4'){
alp[i]='d';
}
else if(str.charAt(i)=='5'){
alp[i]='e';
}
else if(str.charAt(i)=='6'){
alp[i]='f';
}
else if(str.charAt(i)=='7'){
alp[i]='g';
}
else if(str.charAt(i)=='8'){
alp[i]='h';
}
else if(str.charAt(i)=='9'){
alp[i]='i';
}
else if(str.charAt(i)=='0'){
alp[i]='j';
}
else{
alp[i]=str.charAt(i);
}
}

out.println("<tr>");
out.println("<td>");
out.println("Convertion");
out.println("</td>");
out.println("<td>");
out.println(""+str+""); // print the output string "str" , what’s wrong with this?
// when i ran the codes, it didn't show anything.

out.println("</td>");
out.println("</tr>");

}

提前致谢:)

最佳答案

您正在将转换后的字符存储在 alp[] 数组中,但您已打印了 str。所以请打印 alp[] 数组和

使用 ascii 代码执行此操作的更好方法:只需将每个数字作为整数传递给该函数,它就会将这些数字转换为字符并根据需要使用这些字符

private String getCharForNumber(int i) {
return i > 0 && i < 27 ? String.valueOf((char)(i + 64)) : null;
}

并在关闭第一个 for 循环后打印 alp 数组,如下所示

for (int i = 0; i < alp.length; i++) {
System.out.print("" + alp[i] + "");
}

关于java - 数字如何读/拼写为字母?前 : 213 as BAC. (jsp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32919400/

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