[英]Java For loops and char arrays
我正在制作“代码解码器”-因此,共有3行,第一行是参考字符串,第二行是混杂的,然后第三行是根据第一和第二个字符串的匹配来计算的。 例:
敏捷的棕色狐狸跳过了懒狗
UIFARVJDLACSPXOAGPYAKVNQTAPWFSAUIFAMB ZAEPH
XFABSFAWFSZACBEAQFPQMFAEPJOHAWFSZACBEAUIJOHTAIBAIB
我正在做的是获取3个字符串,将它们转换为char数组,然后浏览第一个字符串,找到它在字母数组中的位置,然后在该位置输入另一个数组中第二个字符串的字母在同一个地方。 因此,稍后,我可以在字母表中找到一个字母,并在第二个数组中获取其对应的字母。
问题-没有任何内容输入到第二个数组中...空了。
让我知道是否需要澄清。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter First String:"); // First String
String first_string = br.readLine();
System.out.print("Enter Second String:"); // Second String
String second_string = br.readLine();
System.out.print("Enter Third String:"); // Third String
String third_string = br.readLine();
char[] first = first_string.toCharArray();
char[] second = second_string.toCharArray();
char[] third = third_string.toCharArray();
char[] compare_a = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; //length = 26
char[] compare_b = new char[26]; //length = 26
for(int x =0; x < first.length ; x++){// going through first array
char first_letter = first[x]; // gets letter from first string
for(int z = 0; z < 26; z++){ // looks through alphabet for the position of the letter
if(first_letter == compare_a[z]){ // if the letter is found at a certain position
// System.out.println(first_letter + " = " + compare_a[z] + " " + compare_b[z] + " " + second[x]);
compare_b[z] = second[x]; // insert into the same position on the second array
}
}
}
for(int t =0; t<26; t++){
char m = compare_b[t];
System.out.print(m); //Printing out array of new letters
}
}
注意:
当我尝试将结果输出到字符串时,它正确显示-
letters = letters + second[x];
它只是不进入数组...
应该
compare_b[z] = second[z];
但是
compare_b[z] = second[x];
public static void main(String[] args) {
String first_string = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
String second_string = "UIFARVJDLACSPXOAGPYAKVNQTAPWFSAUIFAMB ZAEPH";
String third_string = "XFABSFAWFSZACBEAQFPQMFAEPJOHAWFSZACBEAUIJOHTAIBAIB";
char[] first = first_string.toCharArray();
char[] second = second_string.toCharArray();
char[] third = third_string.toCharArray();
char[] compare_a = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; //length = 26
char[] compare_b = new char[26]; //length = 26
for (int x = 0; x < first.length; x++) {// going through first array
char first_letter = first[x]; // gets letter from first string
for (int z = 0; z < 26; z++) { // looks through alphabet for the position of the letter
if (first_letter == compare_a[z]) { // if the letter is found at a certain position
// System.out.println(first_letter + " = " + compare_a[z] + " " + compare_b[z] + " " + second[x]);
compare_b[z] = second[z]; // insert into the same position on the second array
}
}
}
for (int t = 0; t < 26; t++) {
char m = compare_b[t];
System.out.print(m); //Printing out array of new letters
}
}
UIFARVJDLACSPXOAGPYAKVNQTA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.