如何计算word中常量的索引并在java中求和?

如何计算word中常量的索引并在java中求和?,java,Java,我无法解决下面提到的问题。我想在控制台中查看从扫描仪插入的字符串的正确索引。但此代码显示第一个字符的索引,该字符查找并不显示字符串索引的正确和: public class task8 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Insert word:"); String str;

我无法解决下面提到的问题。我想在控制台中查看从扫描仪插入的字符串的正确索引。但此代码显示第一个字符的索引,该字符查找并不显示字符串索引的正确和:

public class task8 {

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);

        System.out.println("Insert word:");

        String str;
        str = sc.nextLine();

        char [] consonants = {'b', 'c', 'd', 'f','g','j','h','q','x','z','l','m','p','s','r' , 'n', 't'};
        int conlenth = consonants.length;
        int sum_ind =0;


        for (int s=0; s < str.length(); s++){
            for ( int i = 0; i < conlenth ; i++){
                char ch_cons = consonants[i];
                char ch_str = str.charAt(s);
                int con_ind = str.indexOf(ch_str);
                sum_ind = sum_ind + con_ind;
                if (ch_str == ch_cons){
                    System.out.println( "cons. = " + ch_cons + " index = " + con_ind );

                }
            }
        }
        System.out.println("summ index = " + sum_ind);
    }
}
公共类任务8{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“插入单词:”);
字符串str;
str=sc.nextLine();
char[]辅音={'b','c','d','f','g','j','h','q','x','z','l','m','p','s','r','n','t'};
int conlenth=辅音长度;
int sum_ind=0;
对于(int s=0;s
对于我来说,您只需将
if
测试中的增量移动到仅当我们有匹配项时的增量,如下所示:

...
int con_ind = str.indexOf(ch_str);
if (ch_str == ch_cons) {
    sum_ind += con_ind;
    System.out.println( "cons. = " + ch_cons + " index = " + con_ind );
}
for (int s=0; s < str.length(); s++){
    char ch_str = str.charAt(s);
    switch (ch_str){
        case 'b':
        case 'c':
        ...
        case 'z':
            sum_ind += s;
            System.out.println( "cons. = " + ch_cons + " index = " + s );
            break;
    }
}
响应更新:

Insert word:
hello
cons. = h index = 0
cons. = l index = 2
cons. = l index = 3
summ index = 5
您的程序中还有一个问题,您不应该使用
str.indexOf(ch_str)
来获取当前字符的索引,因为它将为您提供与完整
字符串
匹配的第一个字符的索引,如果您在输入
字符串
中有几次相同的字符,这是不正确的,您应该简单地使用
s
,因为您已经迭代了
字符串的字符

正确的代码如下:

main: for (int s=0; s < str.length(); s++){
    char ch_str = str.charAt(s);
    for ( int i = 0; i < conlenth ; i++){
        char ch_cons = consonants[i];
        if (ch_str == ch_cons){
            sum_ind += s;
            System.out.println( "cons. = " + ch_cons + " index = " + s );
            continue main;
        }
    }
}
更快的方法是使用
switch
语句,而不是内部循环,如下所示:

...
int con_ind = str.indexOf(ch_str);
if (ch_str == ch_cons) {
    sum_ind += con_ind;
    System.out.println( "cons. = " + ch_cons + " index = " + con_ind );
}
for (int s=0; s < str.length(); s++){
    char ch_str = str.charAt(s);
    switch (ch_str){
        case 'b':
        case 'c':
        ...
        case 'z':
            sum_ind += s;
            System.out.println( "cons. = " + ch_cons + " index = " + s );
            break;
    }
}
for(int s=0;s
你希望得到什么结果?嗨,尼古拉斯!我不想得到:插入词:你好const=h,index=0 const=l,index=2 const=l,index=3和index=5。你可能想考虑使用<代码>流<代码> > <代码> char []/Cord>,以将单词减为辅音,然后计算任何内容。remains@IslamIsmayilov如果答案正确,请单击大的“更正/勾选”谢谢你说了声谢谢,并把问题标记为已解决。谢谢你,尼古拉斯。干得好。再次非常感谢。这很管用。还有什么简单的方法,你能给点建议吗?