在处理 3 问题中将莫尔斯电码编码器转换为解码器 [英] Converting Morse Code encoder to decoder in Processing 3 Problem

查看:64
本文介绍了在处理 3 问题中将莫尔斯电码编码器转换为解码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在处理 3 上做莫尔斯电码解码器.我有莫尔斯电码编码器的代码.但我正在努力将编码器转换为解码器.

I'm trying to do Morse Code Decoder on Processing 3. I have codes for Morse Code Encoder. But I'm struggling about convert encoder to decoder.

我也想通过点击输入摩尔斯电码,但它会在转换后进入下一步.

Also I want to enter Morse Code with clicking, but it's going to be next step after the conversion.

我试图将输入和输出以及字母改为莫尔斯,但在代码的最后一部分,我被卡住了.

I tried to change inputs and outputs and alphabets to the morse, but in the last section of the code, i stucked.

这是原始编码器:https://www.openprocessing.org/sketch/134812/

这是我修改后的解码器代码:

Here is my modified decoder code:

String textInput;
String textOutput;

void setup() {
  textInput = decodeMorseCode(textInput);
  textOutput = "";

   println("Morse code: " + textInput);
  println("Text output: " + textOutput);

}

void draw() {
}

String decodeMorseCode(String in_string) {

  String MorseCodeInput = in_string.toLowerCase();
  String TextOutput =  new String();

  String[] MorseCodeArray = {
    "._", "_...", "_._.", "_..", ".", ".._.", "__.", "....", "..", ".___", "_._", "._..", "__", 
    "_.", "___", ".__.", "__._", "._.", "...", "_", ".._", "..._", ".__", "_.._", "_.__", "__.."
  };

  String[] AlphabetArray = {
    "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"
  };


  for (int i=0; i<TextInput.length(); i++) {
    for (int j=0; j<AlphabetArray.length; j++) {
      if (String.valueOf(TextInput.charAt(i)).equals(AlphabetArray[j])) {
        MorseCode += MorseCodeArray[j] + " " ;
      }
    }
  }
  return MorseCode;
}

我在for (int i=0; i

如果它可以正常工作,它应该将莫尔斯解码为文本.

I'm getting an error on the line "for (int i=0; i

If it's going to be work properly it should decode morse to text.

推荐答案

我很乐意帮助你解决这个问题,但首先我必须说这个社区不是教基础知识的.我建议您访问此网站并按照他们的说明了解有关一般处理和编程的更多信息.

I'll gladly help you through this, but first I must say that this community isn't about teaching the basics. I suggest you visit this website and follow their instructions to learn more about Processing and programming in general.

我非常愿意.

以下是您可能了解或不了解的一些关于处理的基本知识:

Here's some basic things you may or may not know about Processing:

Processing 有两个非常重要的方法:setup()draw().

Processing has 2 very important methods: setup() and draw().

运行程序时,Processing 首先读取所有全局变量.然后它读取setup(),然后是draw().最后一个是控制循环,每秒运行固定次数.这是大部分魔法发生的地方.

When you run your program, Processing first read all the global variables. Then it reads setup(), and then draw(). The last one is a controlled loop which run a fixed number of times per second. It's where most of the magic happens.

setup() 主要用于准备"一切,以便 draw() 循环准备就绪.

setup() is mostly used to "prepare" everything so the draw() loop is ready to go.

我查看了编码器的代码.它接受一个输入(字母、单词等)并给出一个输出(摩尔斯电码).编码器应该是相反的:你给程序一些莫尔斯电码,它给你一个翻译.

I took a look at the encoder's code. It takes an input (letters, words, etc) and gives an output (morse code). An encoder should be the opposite: you give the program some morse code, and it gives you a translation.

现在,解决手头的问题:

Now, to the problem at hand:

还记得我说过不要只是说你有错误,告诉我们错误是什么".这很有帮助.我在 Processing 中运行了您的代码,这不是我认为是第一个的问题(好吧,我认为是问题,但不是此时使您的代码崩溃的问题).

Remember when I said "Don't just say that you got an error, tell us what the error says". That helps a lot. I ran your code in Processing, and it wasn't the problem I though it was first (well, the thing I though was a problem, but not the one which crashes your code at this point).

以下是导致代码崩溃的问题,以及如何处理这些问题.阅读本文有望帮助您了解 Processing,但实际上,请学习教程.

Here are the problems which crash your code, and how to deal with them. Reading this will hopefully help you learn about Processing, but, really, do the tutorials.

  1. 变量 TextInput 不存在.相反,您将其命名为 MorseCodeInput,但没有在代码中更改它.有两种不同的简单解决方法,请选择一种:

  1. The variable TextInput doesn't exist. Instead you named it MorseCodeInput, but didn't change it in the code. There are 2 different easy fix to this, choose one:

  • 您可以将变量 MorseCodeInput 重命名为 TextInput.
  • 您可以在 decodeMorseCode 方法中将 TextInput 的每个实例重命名为 MorseCodeInput.
  • You can rename the variable MorseCodeInput as TextInput.
  • You can rename every instance of TextInput as MorseCodeInput in the decodeMorseCode method.

您应该使用 TextOutput 的地方,而是使用了 MorseCode.您必须在以下两个位置将它们重命名为 TextOutput:

Where you should use TextOutput, instead you used MorseCode. You have to rename those as TextOutput at these two places:

  • MorseCode += MorseCodeArray[j] + " " ; => TextOutput += MorseCodeArray[j] + " " ;
  • return MorseCode; => return TextOutput;
  • MorseCode += MorseCodeArray[j] + " " ; => TextOutput += MorseCodeArray[j] + " " ;
  • return MorseCode; => return TextOutput;

setup() 方法中,将输入初始化为一些实际的莫尔斯电码.

In the setup() method, initialize the input to some actual morse code.

  • textInput = "_._. ___ _. __. ._. ._ _ .._ ._.. ._ _ .. ___ _. ...";

textOutput 变量应该是翻译的结果.在setup()方法中,这样使用:

The textOutput variable should be the result of the translation. In the setup() method, use it this way:

  • textOutput = decodeMorseCode(textInput);

耶!没有崩溃!但是也没有输出?为什么?有两个原因:

Yay! No crash! But no output either? Why? There are two reasons:

  • 首先是因为算法正在寻找字母,我们给了它点和线.
  • 第二,因为它一次只处理一个字符,这对于一个字母是几个点和线(每个点和线一个字符)的摩尔斯电码来说没有意义.

所以...这就是你的立场.代码正在运行,但您仍有几个问题需要解决.我会给你其中一个,因为初学者很难弄清楚.

So... That's where you stand. The code is running, but you still have several issues to address. I'll give you one of these, because it'll be kinda hard to figure out for a beginner.

以下是获取莫尔斯电码字符串数组的方法.在 decodeMorseCode() 函数中,像这样初始化 TextInput 变量:

Here's how to get an array of Strings for your morse code. In the decodeMorseCode() function, initialize the TextInput variable like this:

String[] TextInput = split(in_string, ' ');

还有那些行:

for (int i=0; i<TextInput.length; i++) {
// ...
if (String.valueOf(TextInput[i]).equals(AlphabetArray[j])) {

您仍然没有输出,但现在您只需要弄清楚如何将莫尔斯电码与莫尔斯电码数组进行比较以获得适当的字母,而不是相反.如果您尝试失败,请告诉我,我会助您一臂之力.

You will still have no output, but now you only have to figure out how to compare the morse code to the morse code array to get the appropriate letter, and not the other way around. Let me know if you try and fail, and I'll give you a hand.

玩得开心.

这篇关于在处理 3 问题中将莫尔斯电码编码器转换为解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆