Java:如何在If语句中添加数字(在文章中解释)

Java:如何在If语句中添加数字(在文章中解释),java,if-statement,variables,add,Java,If Statement,Variables,Add,我正在为大学做一些家庭作业 我必须在int数组上迭代一个字符串来解码匹配的符号 我需要您帮助解决以下问题: 这是我现在的代码: public static void main(String[] args) { String s = "| || | |||| |||| || || | ||| || ||"; int result = 0; int[] num = {0, 1, 2, 4, 7};

我正在为大学做一些家庭作业

我必须在int数组上迭代一个字符串来解码匹配的符号

我需要您帮助解决以下问题:

这是我现在的代码:

public static void main(String[] args) {
            String s = "| || |  ||||  |||| || || | ||| || ||";

            int result = 0;
            int[] num = {0, 1, 2, 4, 7};
            String over = "";
            String[] parts = s.split("(?<=\\G.{6})");
            for(int i=0; i<parts.length; i++) {
              parts[i] = parts[i].substring(0, 5);
              over = parts[i];
              for(int j = 0; j < num.length; j++) {
                  if(over.charAt(j) == ' ') {
                      result = num[j];

                  }                   
              }
              System.out.println(result);
}



}
publicstaticvoidmain(字符串[]args){
字符串s=“||||| | | | | | | | | | |”;
int结果=0;
int[]num={0,1,2,4,7};
串在“”上;

String[]parts=s.split((?添加一个中间变量来计算循环的最终结果应该可以做到这一点。
这对我来说非常有效

public static void main(String[] args) {
            String s = "| || |  ||||  |||| || || | ||| || ||";

            int result = 0;
            int[] num = {0, 1, 2, 4, 7};
            String over = "";
            String[] parts = s.split("(?<=\\G.{6})");
            for(int i=0; i<parts.length; i++) {
              parts[i] = parts[i].substring(0, 5);
              over = parts[i];
              int intermediateResult = 0;
              for(int j = 0; j < num.length; j++) {
                  if(over.charAt(j) == ' ') {
                      intermediateResult += num[j];

                  }                   
              }
              result = intermediateResult;
              System.out.println(result);
       }
}
publicstaticvoidmain(字符串[]args){
字符串s=“||||| | | | | | | | | | |”;
int结果=0;
int[]num={0,1,2,4,7};
串在“”上;

String[]parts=s.split((?如上所述,您只是忘记了对编码值的4个有用部分求和,如8=1+7;

但有关代码的更多说明:

如果您使用
int[]num={0,1,2,4,7,0};
您甚至不需要拆分字符串s,假设它总是有6个元素的倍数。然后您可以使用一个简单的while或for循环来表示s的全长(特别是如果s的长度是恒定的,或者可以将其填充为恒定长度,以防您可能有5或6位数字,而不是始终有6位)。
在这个循环中,您可以使用索引
i%6
,也可以在每次达到6时将另一个索引j返回到0,但速度应该是相等的。您需要该索引来打印数字,将中间总和重置为0,并在实际生活中在重置之前将其与邮政编码总额相加


但更好的是,您可以使用
int[]num={0,100000,200000,400000,700000,0,10000,20000,40000,70000,0,1000,2000,4000,7000,0,0,100,200,400,700,0,0,0,10,20,40,70,0,0,1,2,4,7,0};
然后您只需要计算s和num之间的“叉积”。这是一个非常简单的
(charAt(i)='')
或者,如果直接将s编码为01001011000011,甚至可以成为对实际叉积函数的调用…也可以是在数组中(0=''|',1='')。

“结果不会将第一个结果添加到下一个结果”您是否尝试过
结果=结果+num[j]
?很抱歉,这不是很清楚。你为什么认为输出应该是811424?是的,但这会把其他数字搞乱。我的代码不会在结果中将num数组的数字相加,只要一个部分中有2个匹配项。我希望这有意义给定的字符串是编码的邮政编码。在德国,将此编码是一件事。以下是一些信息(德语,很抱歉)这是太多无用的中间变量(超过,intermediateResult,可能更多),但我确认算法修复我不打算在这个答案中进行代码清理。但我同意你的观点!
public static void main(String[] args) {
            String s = "| || |  ||||  |||| || || | ||| || ||";

            int result = 0;
            int[] num = {0, 1, 2, 4, 7};
            String over = "";
            String[] parts = s.split("(?<=\\G.{6})");
            for(int i=0; i<parts.length; i++) {
              parts[i] = parts[i].substring(0, 5);
              over = parts[i];
              int intermediateResult = 0;
              for(int j = 0; j < num.length; j++) {
                  if(over.charAt(j) == ' ') {
                      intermediateResult += num[j];

                  }                   
              }
              result = intermediateResult;
              System.out.println(result);
       }
}
public static void main(String[] args) {
            String s = "| || |  ||||  |||| || || | ||| || ||";

            int[] num = {0, 1, 2, 4, 7};
            String[] parts = s.split("(?<=\\G.{6})");
            for(int i=0; i<parts.length; i++) {
              parts[i] = parts[i].substring(0, 5);
              int result = 0;
              for(int j = 0; j < num.length; j++) {
                  if(parts[i].charAt(j) == ' ') {
                      result += num[j];

                  }                   
              }
              System.out.println(result);
       }
}