java - 如何将数字转换为字母(A = 0、B = 1、C = 2 等...)
问题描述
实际上我很抱歉代码不完整我想这可能更好..我想要的是在算法完成后我希望能够打印出数字输出并将数字输出转换为字母..很可能就像密码
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
String ct;
String k;
int sum = 0;
System.out.print("Enter Text: ");
ct = scanner.nextLine();
int arr1[] = new int[ct.length()];
for(int j = 0; j < ct.length(); j++)
{
arr1[j] += ( (int)ct.charAt(j) - 97);
System.out.println("Value of Text at["+j+"]: "+arr1[j]);
}
//user input key
System.out.print("Enter random word: ");
k = scanner.nextLine();
for(int i = 0; i < k.length(); i++)
{
System.out.println( (int)k.charAt(i) - 97 );
sum = sum + ( +(int)k.charAt(i) - 97 );
}
System.out.println("Sum of all letter in the random word: "+sum);
for(int i = 0; i < ct.length(); i++)
{
System.out.print(arr1[i]+ " ");
arr1[i] += sum;
if(arr1[i] > 25)
{
arr1[i] -= 25;
if(arr1[i] > 25)
{
arr1[i] -= 25;
}
}
}
System.out.println();
for(int i = 0; i < ct.length(); i++)
{
System.out.print(arr1[i]+ " ");
}
}
输出
Enter Text: kind
Value of Text at[0]: 10
Value of Text at[1]: 8
Value of Text at[2]: 13
Value of Text at[3]: 3
Enter random word: evil
4
21
8
11
Sum of all letter in the random word: 44
10 8 13 3
4 2 7 22
解决方案
你可以使用一个字符数组
char[] letters = "ABCD".toCharArray();
您想要的字母位于您的号码在数组中的位置。
System.out.println(letters[0]);
会打印'A'</p>
这仅适用于您希望 A 从 0 而不是 97 开始。
推荐阅读
- node.js - 使用 node.js 和 MongoDB 对 Web 应用程序进行 Dockerizing 时的代码 14
- .net - 在 .NET 框架中使用 VST 插件效果
- java - 如何通过 java 代码连接到 github 企业存储库?
- c# - 如何用类属性填充字典值并随时更新
- sass - 从 scss 文件中分离某些属性
- android - 在 OkHttp3 中检测 WiFi 连接丢失
- django - 如何在 CKEditor 的 CodeMirror 插件中添加 Django 语言模式?
- node.js - 如何将 JSON 的批量写入项目到本地 DynamoDB
- regex - 使用 .htaccess 阻止 Googlebot 访问以 4-6 位数字结尾的 URL,正则表达式?
- mysql - default-libmysqlclient-dev 还是 libperconaserverclient20-dev?