Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(高中Java家庭作业帮助)关于将英文字母转换为摩尔斯电码的困惑_Java_Arrays_String - Fatal编程技术网

(高中Java家庭作业帮助)关于将英文字母转换为摩尔斯电码的困惑

(高中Java家庭作业帮助)关于将英文字母转换为摩尔斯电码的困惑,java,arrays,string,Java,Arrays,String,我正在为我的高中计算机科学课做一个项目,我遇到了一些困惑。我被要求写一个程序,将用户输入的信息从英语翻译成摩尔斯电码。我已经完成了大部分作业,但我似乎不知道如何将用户的信息转换成摩尔斯电码 import java.util.*; import java.io.File; import java.io.IOException; public class MorseCode { public MorseCode() { } //Reads in file containing Morse cod

我正在为我的高中计算机科学课做一个项目,我遇到了一些困惑。我被要求写一个程序,将用户输入的信息从英语翻译成摩尔斯电码。我已经完成了大部分作业,但我似乎不知道如何将用户的信息转换成摩尔斯电码

import java.util.*;
import java.io.File;
import java.io.IOException;
public class MorseCode
{
public MorseCode()
{

}

//Reads in file containing Morse code and stores into an array of Strings
public static String [] readFile() throws IOException
{
    String [] codes = new String[36];
    int index = 0;
    Scanner fileScanner = new Scanner(new File("morsecode.txt"));

    while( fileScanner.hasNextLine() )
    {
        codes[index] = fileScanner.nextLine();
        index++;
    }

    return codes;
}

//Converts the array of morse codes to an array of its corresponding letter
public static String [] findChars(String [] morseCode)
{
    String [] alphabet = {"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"};

    for( int index = 0; index < morseCode.length; index++)
    {
        morseCode[index] = alphabet[index];
    }

    return morseCode;
}
}
import java.util.*;
导入java.io.File;
导入java.io.IOException;
公共类莫尔斯电码
{
公共莫尔斯电码()
{
}
//读入包含摩尔斯电码的文件并存储到字符串数组中
公共静态字符串[]readFile()引发IOException
{
字符串[]代码=新字符串[36];
int指数=0;
Scanner fileScanner=新扫描仪(新文件(“morsecode.txt”);
while(fileScanner.hasNextLine())
{
代码[索引]=fileScanner.nextLine();
索引++;
}
返回码;
}
//将摩尔斯电码数组转换为对应字母的数组
公共静态字符串[]findChars(字符串[]morseCode)
{
字符串[]字母={“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"};
for(int index=0;index
主要的 到目前为止,只是用来测试,以确保一切正常。摩尔斯电码及其字母和数字等效输出均正确

import java.util.*;
import java.io.File;
import java.io.IOException;
public class MorseCodeTester
{
public static void main(String [] args) throws IOException
{
    Scanner in = new Scanner(System.in);
    //Original Morse Code
    String [] morseCode = MorseCode.readFile();

    //Conversion to standard english letters
    String [] toConvert = MorseCode.readFile();
    String [] codeToLetters = MorseCode.findChars(toConvert);

    for( int index = 0; index < morseCode.length; index++)
    {
        System.out.println(morseCode[index]);
    }

    System.out.println();

    for( int index = 0; index < morseCode.length; index++)
    {
        System.out.println(codeToLetters[index]);
    }
}
}
import java.util.*;
导入java.io.File;
导入java.io.IOException;
公共类Morsecodester
{
公共静态void main(字符串[]args)引发IOException
{
扫描仪输入=新扫描仪(系统输入);
//原始莫尔斯电码
字符串[]morseCode=morseCode.readFile();
//转换成标准英文字母
字符串[]toConvert=MorseCode.readFile();
String[]codeToLetters=MorseCode.findChars(toConvert);
for(int index=0;index

如前所述,我的困惑来自于用户输入所需信息后将英语实际转换为摩尔斯电码。如果有人能给我指出正确的方向,我将不胜感激。

您可以将每个字符及其对应的摩尔斯电码字符串存储在地图中。e、 g

HashMap<String, String> codes = new HashMap<String, String>();
codes.put("a", ".-");
codes.put("b", "-...");
HashMap code=newhashmap();
代码。将(“a”、“-”)放入;
代码。付诸表决(“b”和“-…”);
然后,在转换字符串时,可以使用映射为要转换的字符串的每个字符找到相应的摩尔斯电码字符串

例如:

String str = "To be converted to morse";
StringBuilder morseSB = new StringBuilder();
for(int i=0; i< str.length(); i++)
{
    morseSB.append(codes.get(Character.toString(str.charAt(i)).toLowerCase()));
}

String morseResult = morseSB.toString();
String str=“要转换成莫尔斯”;
StringBuilder morseb=新的StringBuilder();
对于(int i=0;i

注意:您需要添加一些预防性检查。e、 g.检查codes.get()的结果是否为空,您可能需要处理空格和句点等其他字符。

您可以将每个字符及其对应的摩尔斯电码字符串存储在地图中。e、 g

HashMap<String, String> codes = new HashMap<String, String>();
codes.put("a", ".-");
codes.put("b", "-...");
HashMap code=newhashmap();
代码。将(“a”、“-”)放入;
代码。付诸表决(“b”和“-…”);
然后,在转换字符串时,可以使用映射为要转换的字符串的每个字符找到相应的摩尔斯电码字符串

例如:

String str = "To be converted to morse";
StringBuilder morseSB = new StringBuilder();
for(int i=0; i< str.length(); i++)
{
    morseSB.append(codes.get(Character.toString(str.charAt(i)).toLowerCase()));
}

String morseResult = morseSB.toString();
String str=“要转换成莫尔斯”;
StringBuilder morseb=新的StringBuilder();
对于(int i=0;i
注意:您需要添加一些预防性检查。e、 g.检查code.get()的结果是否为空,您可能需要处理空格和句点等其他字符。

此函数用于将“Hello world”转换为莫尔斯电码

public static String charToMorse( String s ){
    if( s.equals( "H" ) || s.equals("h"))
        return "....";
    else if ( s.equals( "E" ) || s.equals("e"))
        return "."
    // other alphabet will placed here
}
并编写另一个方法遍历字符串的字符

public static void showMorse( String s ){
    for( int i=0 ; i < s.length() ; i++ )
        System.out.printf( "%s " , charToMorse( s[i] ) );
    System.out.println();
}
公共静态void showmores(字符串s){
对于(int i=0;i
此图显示您需要的摩尔斯电码

此函数用于将“Hello world”转换为莫尔斯电码

public static String charToMorse( String s ){
    if( s.equals( "H" ) || s.equals("h"))
        return "....";
    else if ( s.equals( "E" ) || s.equals("e"))
        return "."
    // other alphabet will placed here
}
并编写另一个方法遍历字符串的字符

public static void showMorse( String s ){
    for( int i=0 ; i < s.length() ; i++ )
        System.out.printf( "%s " , charToMorse( s[i] ) );
    System.out.println();
}
公共静态void showmores(字符串s){
对于(int i=0;i
此图显示您需要的摩尔斯电码

你用谷歌搜索它吗?你会用谷歌搜索吗?那会更好:)哈哈。虽然这可能是解决这个问题的最好方法,但我认为它实际上可能对OP没有多大帮助。为什么不呢?抱歉,用一种算法将英语转换成莫尔斯语是不可能的,而且需要某种映射或查找表。考虑到目前为止他们是如何尝试这个问题的,我更倾向于暗示OPs当前的Java知识。那更好:)哈哈。虽然这可能是解决这个问题的最好方法,但我认为它实际上可能对OP没有多大帮助。为什么不呢?抱歉,用一种算法将英语转换成莫尔斯语是不可能的,而且需要某种映射或查找表。考虑到目前为止他们是如何尝试这个问题的,我更倾向于暗示OPs当前的Java知识。