还记得你在玩游戏时索引你名字的每个单词并将它们加在一起形成一个秘密数字吗?就像aay
会1+1+25=27
我试图用java中的各种方法做同样的事情,但我失败了。让我先分享我的脚本,然后告诉我我尝试了什么。
class test{
public static void main(String args[]){
int c = 3;
String s = "c";
///now this is what all i tried:
int value1 = (int)(s);
///i tried a cast, but that failed.
String d = "4";
int value2 = Integer.parseInt(d);
///when i try this, it correctly converts the string to an integer.
Integer.parseInt(s);
///but when i try the above, it fails to do the same.
}
}
在这段代码中,我尝试了 2 种不同的方法,这两种方法都以我想要但不准确的方式工作。
问题是它无法重新组织 c 是一个具有整数值的变量。
那么,有什么捷径可以做到吗?此外,现在字符串只有 1 位长,一旦用户输入他或她的姓名,我将使用 for 循环将所有字母完全循环。
如果没有任何捷径,我唯一的选择是做一个 if 语句,如:
if(letter=a;){
value=1;
}
或类似的东西?
感谢您的帮助!