Java 当我编译代码时,它不会';不要显示特定的东西
因此,我最终设法修复并完成了我自己的摩尔斯电码翻译程序的源代码,从英语到摩尔斯电码,反之亦然。英文到摩尔斯电码工作得很好,没有问题,但是摩尔斯电码到英文只能输入,但翻译后不会显示英文。正如我所说,没有错误,它编译得很好,只是不显示。这是我的代码,我希望你们能帮助我意识到什么是错的,我将非常感谢。谢谢Java 当我编译代码时,它不会';不要显示特定的东西,java,Java,因此,我最终设法修复并完成了我自己的摩尔斯电码翻译程序的源代码,从英语到摩尔斯电码,反之亦然。英文到摩尔斯电码工作得很好,没有问题,但是摩尔斯电码到英文只能输入,但翻译后不会显示英文。正如我所说,没有错误,它编译得很好,只是不显示。这是我的代码,我希望你们能帮助我意识到什么是错的,我将非常感谢。谢谢 public class Project1 { public static void main( String [] args ) { System.out.println(); c
public class Project1
{
public static void main( String [] args )
{
System.out.println();
choice();
}
public static void choice()
{
int user_choice = 0;
user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code to English");
if(user_choice == 1)
{
String output = new String();
String inital = new String();
inital = english_to_morse();
for( int k = 0; k < inital.length(); k++)
{
output += morse(inital.charAt( k ));
}
System.out.print(output);
}
if(user_choice == 2)
{
String output2 = new String();
String inital2 = new String();
inital2 = morse_to_english();
for( int k = 0; k < inital2.length(); k++)
{
output2 += english(String.valueOf(inital2.charAt( k )));
}
System.out.print(output2); ///This is where the display is
}
}
public static String english_to_morse()
{
String user_input = new String();
user_input = Input.getString("Enter a phrase and I'll convert it to Morse Code");
return user_input.toLowerCase();
}
public static String morse_to_english()
{
String user_input = new String();
user_input = Input.getString("Enter a phrase in Morse Code and I'll convert it to English");
return user_input.toLowerCase();
}
public static String morse(char letter)
{
String output = new String();
char[] alphabet_numbers = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };
String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };
for( int j = 0; j < alphabet_numbers.length; j++ )
{
if (alphabet_numbers[j]==letter)
{
output = morse_code[j];
}
}
return output + " ";
}
public static String english(String letter)
{
String output = new String();
String alphabet_numbers[] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " };
String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };
for( int j = 0; j < morse_code.length; j++ )
{
if (morse_code[j]==letter)
{
output = alphabet_numbers[j];
}
}
return output + " ";
}
}
公共类项目1
{
公共静态void main(字符串[]args)
{
System.out.println();
选择();
}
公共静态无效选择()
{
int user_choice=0;
user_choice=Input.getInt(“如果要将英语更改为摩尔斯电码,请输入1,如果要将摩尔斯电码更改为英语,请输入2”);
如果(用户选择==1)
{
字符串输出=新字符串();
String inital=新字符串();
inital=英语从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句从句;
对于(int k=0;k需要改进的地方很多,但最重要的可能是你的字符串在英语中的比较(…)
:如果你使用==
比较两个字符串
if (morse_code[j]==letter)
如果对象引用相同,而不是字符串,则这将仅产生true
。你应该使用
if (morse_code[j].equals(letter))
<>这将极大地提高你发现任何字符的机会(显然你现在没有,你应该考虑打印一些错误信息,如果是<代码> .-< /代码>系列不能在<代码> MySeScript代码< /代码> s中找到。所有的<代码>新的字符串()/<代码>调用是什么?你只是想让垃圾收集者赚大钱吗?大多数情况下,这可能是这么多问题的重复。让我猜一下:输出由'e'
s和'n'
s组成,对吗?。输出由用户在您尝试调试它之前确定?在不同的地方插入println语句来查看发生了什么?我这样做了,它会显示单词,但当我把多个点和破折号放在一起时,它只会显示单个点或单个点的英文字母dash@user3474526你知道摩尔斯电码中1到3个字符被转换成一个英文字母吗?您的转换使用inital2.charAt(k)
。你认为这是怎么回事?嗯,也许你对莫尔斯电码的“编码”不完整?摩尔斯电码中的字母之间有一个停顿,以确定字母何时完成。你必须“标记”你的莫尔斯“字符串”,将其拆分为莫尔斯字母。