Java 类,该类将短语中字符的字符串值转换为摩尔斯电码
我不明白为什么我能够用一个非常相似的循环打印这些字符串值,但它不会像我假设的replace方法那样遍历并替换字母。以下是输出: --但是,正如你可能清楚地看到的那样,本应翻译的短语并非如此。我打印相互对齐的数组和值。输入是重新打印的(未更改),尽管它是要调整为morse的字符串值。如果有人能够解释问题所在,我将不胜感激 .txt文件中的文本值也如下所示:Java 类,该类将短语中字符的字符串值转换为摩尔斯电码,java,string,class,replace,logic,Java,String,Class,Replace,Logic,我不明白为什么我能够用一个非常相似的循环打印这些字符串值,但它不会像我假设的replace方法那样遍历并替换字母。以下是输出: --但是,正如你可能清楚地看到的那样,本应翻译的短语并非如此。我打印相互对齐的数组和值。输入是重新打印的(未更改),尽管它是要调整为morse的字符串值。如果有人能够解释问题所在,我将不胜感激 .txt文件中的文本值也如下所示: .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .
.-
-...
-.-.
-..
.
..-.
--.
....
..
.---
-.-
.-..
--
-.
---
.--.
--.-
.-.
...
-
..-
...-
.--
-..-
-.--
--..
.----
..---
...--
....-
.....
-....
--...
---..
----.
-----
然后是课堂:
import java.util.*;
import java.io.*;
public class MorseCode
{
public static void main(String []args) throws IOException
{
String characters[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
String morseCharacter[] = new String[(characters.length)];
Scanner in = new Scanner(System.in);
System.out.println();
System.out.print("Enter a phrase you wish to translate to morse: ");
String entry = in.nextLine();
File fileName1 = new File("morseCode.txt");
Scanner inFile = new Scanner(fileName1);
int counter1 = 0;
while(inFile.hasNextLine() && counter1 < (characters.length))
{
morseCharacter[counter1] = inFile.nextLine();
morseCharacter[counter1] += " ";
counter1++;
}
String result = "";
for(int x = 0; x < (morseCharacter.length); x++)
{
result = entry.replaceAll(characters[x], morseCharacter[x]);
}
for(int x = 0; x < (morseCharacter.length); x++)
{
System.out.println(characters[x] + " = " + morseCharacter[x]);
}
System.out.println(result);
}
}
import java.util.*;
导入java.io.*;
公共类莫尔斯电码
{
公共静态void main(字符串[]args)引发IOException
{
字符串[]={“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”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“0”};
字符串morseCharacter[]=新字符串[(characters.length)];
扫描仪输入=新扫描仪(系统输入);
System.out.println();
System.out.print(“输入一个你想翻译成莫尔斯的短语:”);
String entry=in.nextLine();
File fileName1=新文件(“morseCode.txt”);
扫描仪内嵌=新扫描仪(文件名1);
int计数器1=0;
while(infle.hasNextLine()&计数器1<(characters.length))
{
morseCharacter[counter1]=infle.nextLine();
morseCharacter[counter1]+=“”;
计数器1++;
}
字符串结果=”;
对于(int x=0;x<(morseCharacter.length);x++)
{
结果=entry.replaceAll(字符[x],字符[x]);
}
对于(int x=0;x<(morseCharacter.length);x++)
{
System.out.println(字符[x]+“=”+morseCharacter[x]);
}
系统输出打印项次(结果);
}
}
您只应用最后一个字符,因为您总是从条目
替换,而不是从以前的结果
替换
试试这个:
String result = entry;
// ^^^^^
for(int x = 0; x < (morseCharacter.length); x++)
{
result = result.replaceAll(characters[x], morseCharacter[x]);
} // ^^^^^^
字符串结果=输入;
// ^^^^^
对于(int x=0;x<(morseCharacter.length);x++)
{
result=result.replaceAll(字符[x],字符[x]);
} // ^^^^^^
您应该通过读取和替换将代码包装到循环中。大概是这样的:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MorseCode {
public static void main(String[] args) throws IOException {
String characters[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
int counter1 = 0;
Scanner inFile = new Scanner(new File("morseCode.txt"));
String morseCharacter[] = new String[(characters.length)];
while (inFile.hasNextLine() && counter1 < (characters.length)) {
morseCharacter[counter1] = inFile.nextLine();
morseCharacter[counter1] += " ";
counter1++;
}
for (int x = 0; x < (morseCharacter.length); x++) {
System.out.println(characters[x] + " = " + morseCharacter[x]);
}
Scanner in = new Scanner(System.in);
System.out.print("\nEnter a phrase you wish to translate to morse: ");
while (in.hasNextLine()) {
String line = in.nextLine();
for (int x = 0; x < (morseCharacter.length); x++) {
line = line.replaceAll(characters[x], morseCharacter[x]);
}
System.out.println(line);
}
}
}
导入java.io.File;
导入java.io.IOException;
导入java.util.Scanner;
公共类莫尔斯电码{
公共静态void main(字符串[]args)引发IOException{
字符串[]={“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”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“0”};
int计数器1=0;
扫描仪内嵌=新扫描仪(新文件(“morseCode.txt”);
字符串morseCharacter[]=新字符串[(characters.length)];
while(infle.hasNextLine()&计数器1<(characters.length)){
morseCharacter[counter1]=infle.nextLine();
morseCharacter[counter1]+=“”;
计数器1++;
}
对于(int x=0;x<(morseCharacter.length);x++){
System.out.println(字符[x]+“=”+morseCharacter[x]);
}
扫描仪输入=新扫描仪(系统输入);
System.out.print(“\n输入一个你想翻译成莫尔斯的短语:”);
while(在.hasNextLine()中){
String line=in.nextLine();
对于(int x=0;x<(morseCharacter.length);x++){
line=line.replaceAll(字符[x],字符[x]);
}
系统输出打印项次(行);
}
}
}