我想将特定值分配给字母(下面的代码中),然后计算它 由输入字符串提供的总增加输出,我没有得到。例如。 Kamran = 2 + 1 + 4 + 2 + 1 + 5 = 15或abc = 1 + 2 + 3 = 6。 通过这段代码,我只得到最后一位数的输出。 提前致谢!问候!!!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p1 {
static int addition;
static char Kamran;
static int string1;
static int total;
final static int a=1,b=2,c=3,d=4,e=5,f=8,g=3,h=5,i=1,j=1,k=2,l=3,m=4,n=5,o=7,p=8,q=1,r=2,s=3,t=4,u=6,v=6,w=6,x=5,y=1,z=7;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String userInput = in.readLine();
for(int ai=0;ai<userInput.length();ai++){
string1 = (int)userInput.charAt(ai);
}
int adder=0;
if(string1=='k')
{adder = 2 + adder ; }
if(string1=='a')
{adder = 1 + adder ; }
if(string1=='m')
{adder = 4 + adder ; }
if(string1=='r')
{adder = 2 + adder ; }
if(string1=='b')
{adder = 2 + adder ; }
if(string1=='n')
{adder = 5 + adder ; }
total=adder;
System.out.println(total);
}
}
答案 0 :(得分:1)
尝试:
public int desiredMethod(String input, Function<Character, Integer> conversion){
String inputRevised = input.toLowerCase();
int output = 0;
for(int i=0 ; i<inputRevised.length() ; i++)
output = output + conversion.apply(inputRevised.charAt(i));
return output;
}
这样,相同的方法可以使用多个地图字符»整数。
答案 1 :(得分:0)
int adder=0;
for(int ai=0;ai<userInput.length();ai++){
string1 = (int)userInput.charAt(ai);
if(string1=='k')
{adder = 2 + adder ; }
if(string1=='a')
{adder = 1 + adder ; }
if(string1=='m')
{adder = 4 + adder ; }
if(string1=='r')
{adder = 2 + adder ; }
if(string1=='b')
{adder = 2 + adder ; }
if(string1=='n')
{adder = 5 + adder ; }
}
System.out.println(adder);
你不是在循环中添加你只会得到最后一个字符的值。
答案 2 :(得分:0)
试试这种方式
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Test {
/*final static int a = 1, b = 2, c = 3, d = 4, e = 5, f = 8, g = 3, h = 5,
i = 1, j = 1, k = 2, l = 3, m = 4, n = 5, o = 7, p = 8, q = 1,
r = 2, s = 3, t = 4, u = 6, v = 6, w = 6, x = 5, y = 1, z = 7;*/
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String userInput = (in.readLine()).toLowerCase();
HashMap<Character,Integer> hm = new HashMap<Character, Integer>();
hm.put('a', 1); hm.put('b', 2); hm.put('c', 3); hm.put('d', 4); hm.put('e', 5);
hm.put('f', 8); hm.put('g', 3); hm.put('h', 5); hm.put('i', 1); hm.put('j', 1);
hm.put('k', 2); hm.put('l', 3); hm.put('m', 4); hm.put('n', 5); hm.put('o', 7);
hm.put('p', 8); hm.put('q', 1); hm.put('r', 2); hm.put('s', 3); hm.put('t', 4);
hm.put('u', 6); hm.put('v', 6); hm.put('w', 6); hm.put('x', 5); hm.put('y', 1);
hm.put('z', 7);
int total = 0;
for(int ai=0;ai<userInput.length();ai++){
total+= hm.get(userInput.charAt(ai));
}
System.out.println(total);
}
}
答案 3 :(得分:0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p1 {
final static int a=1,b=2,c=3,d=4,e=5,f=8,g=3,h=5,i=1,j=1,k=2,l=3,m=4,n=5,o=7,p=8,q=1,r=2,s=3,t=4,u=6,v=6,w=6,x=5,y=1,z=7;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String userInput = in.readLine();
String input;
int adder = 0;
System.out.println(" userInput " + userInput);
for (int i = 0; i < userInput.length(); i++){
input = userInput.charAt(i) + "";
System.out.println(" input " + input);
if (input.equals("k"))
adder += k;
if (input.equals("a"))
adder += a;;
if (input.equals("m"))
adder += m;;
if (input.equals("r"))
adder += r;
if (input.equals("b"))
adder += b;
if (input.equals("n"))
adder += n;
}
System.out.println(" adder " + adder);
}
}