在一个字符上添加数字以使其成为另一个字符 [英] add number to a character to make it another character
问题描述
我想要一个字符,在其中添加数字并获得另一个char(a+2=c)
int leng,leng2;
String word = "";
String key = "";
String enc ="";
char x;
char z;
char tost;
System.out.println("gimme word");
word=in.next();
System.out.println("gimme key");
key=in.next();
leng=word.length();
leng2=key.length();
for(int i=1;i<=leng;i++)
{
z=word.charAt(i-1);
x=key.charAt(i-1);
int plus=z + x;
tost=(char)plus;
enc=enc+tost;
System.out.println(enc);
System.out.println(tost);
System.out.println((char)z);
System.out.println((char)x);
System.out.println(plus);
System.out.println((char)plus);
}
我希望它打印c
,并且在我的代码中我用charAt
进行打印,因为我有一个完整的字符串,并且我尝试搜索许多解决方案,并尝试了许多事情,但它们都无法令人满意地工作./p>
根据要求打开了完整代码 字符加的方式无效,并说这是一个错误
如果要对整个字符串执行此操作,则可以使用for循环-
String r= "abc";
int x=2;
for(i=0; i<r.length(); i++){
char z= r.charAt(0);
int plus=z+x;
System.out.println((char)plus);
}
查看System.out.println()
上的类型转换.由于plus
是int
,因此必须将其显式转换为char
.
I want to have a char, add number to it and get another char(a+2=c)
int leng,leng2;
String word = "";
String key = "";
String enc ="";
char x;
char z;
char tost;
System.out.println("gimme word");
word=in.next();
System.out.println("gimme key");
key=in.next();
leng=word.length();
leng2=key.length();
for(int i=1;i<=leng;i++)
{
z=word.charAt(i-1);
x=key.charAt(i-1);
int plus=z + x;
tost=(char)plus;
enc=enc+tost;
System.out.println(enc);
System.out.println(tost);
System.out.println((char)z);
System.out.println((char)x);
System.out.println(plus);
System.out.println((char)plus);
}
I want it to print c
and in my code I do it with charAt
because I have a full string and I tried to search for many solutions, and tried myself many things, they all didnt work sadly.
edit: full code is on, as requested the way of char plus doesnt work and says it is an error
If you want to do this for the whole string then you may use a for loop -
String r= "abc";
int x=2;
for(i=0; i<r.length(); i++){
char z= r.charAt(0);
int plus=z+x;
System.out.println((char)plus);
}
Look at the type casting at the System.out.println()
. Since plus
is an int
you have to explicitly cast it to a char
.
这篇关于在一个字符上添加数字以使其成为另一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!