我想爲字母(代碼中的下面)指定特定值,然後計算其輸入字符串提供的總增加輸出,這是我沒有收到的。例如。 Kamran = 2 + 1 + 4 + 2 + 1 + 5 = 15或abc = 1 + 2 + 3 = 6. 通過此代碼,我只能得到最後一位數字的輸出。 在此先感謝!問候!!!Java:如何爲字符串輸入獲取分配的數值
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);
}
}
問題是? – Bozho 2011-12-21 08:51:49
也許你的加法應該發生在循環內部而不是之後? – Thilo 2011-12-21 08:52:39
哪裏使用你的最終靜態變量? – shift66 2011-12-21 08:53:55