由于某种原因,我的循环确实经历了第一次循环运行,并忽略了数组的其余部分,并始终使用相同的值。 我的控制台测试代码:
I-Counter: 0
Run-String: | ||
Beginnung Counter: 0
Char: |
IF: 1
Space:
ELSE: 2
Beginnung Counter: 2
Char: |
IF: 3
Beginnung Counter: 3
Char: |
IF: 4
Space:
ELSE: 5
Temporary: 8
I-Counter: 1
Run-String: |||
Temporary: 8
I-Counter: 2
Run-String: |||
Temporary: 8
I-Counter: 3
Run-String: || |
Temporary: 8
I-Counter: 4
Run-String: | ||
Temporary: 8
I-Counter: 5
Run-String: || |
Temporary: 8
Falscher Code
我真的看不到我的错误。
通过两个循环,然后显示“ Falscher Code”,但预期值为 “ 24118”。
这可能是什么原因?
import java.util.Arrays;
public class LinearCode {
public static void main(String[] args) {
String s = "| || | |||| |||| || || | ||| || ||"; //Richtige PLZ
System.out.println(calc(s));
}
public static String calc(String s) {
String result = "";
String wrong = "Falscher Code";
int test = 0; //modulo-check
int temp = 0; //temporär
int t = 0; //counter for Checking Array
int Counter = 0; // Counter for the Final Array
int [] finalArray = new int[s.length()];
String[] parts = s.split("(?<=\\G.{6})");
for(int i=0; i<parts.length; i++) {
parts[i] = parts[i].substring(0, 5);
}
int [] check = {0,1,2,4,7}; //Checking Array
for(int i = 0; i < parts.length; i++) {
System.out.println("I-Counter: " + i);
String run = parts[i];
System.out.println("Run-String: " + run);
for(int j=0; j < run.length(); j++) {
if(!(run.charAt(j)== ' ')) {
System.out.println("Beginnung Counter: " + t);
t++;
System.out.println("Char: " + run.charAt(j));
System.out.println("IF: " + t);
}else {
if(t<check.length) {
temp += check[t];
t++;
System.out.println("Space: ");
System.out.println("ELSE: " + t);
}else {
break;
}
}
}
System.out.println("Temporary: " + temp);
if(temp == 11) { temp = 0;}
finalArray[Counter] = temp;
Counter++;
}
finalArray = Arrays.copyOf(finalArray, Counter);
for(int i = 0; i < finalArray.length-1; i++) {
test += finalArray[i];
}
test = (10-(test%10));
if(test == finalArray[finalArray.length-1]) {
for(int i = finalArray.length-1; i > 0; i--) {
result += finalArray[i];
}
return result;
}
return wrong;
}
}