我的程序中有一些没有意义的东西。可能是我的 while 语句在它的循环中嵌入了几个不同的 if 语句。但主要错误是在我的while循环之后,我似乎无法弄清楚。我评论了错误在哪里。
这是程序的样子:
import java.util.*;
import java.io.*;
class MorseCode
{
public static void main (String [] args) throws FileNotFoundException
{
Scanner keyboard = new Scanner(new File("myfile.txt"));
String text = " ";
while (keyboard.hasNextLine())
{
text = keyboard.nextLine();
morse(text); // Goes through morse(text);
String code = ""; // String is declared
code = morse(text);
System.out.println(code); // prints the code which is being called upon morse(text)
}
keyboard.close();
}
static String morse(String text)
{
String code = "";
int i = 0;
while (true) {
if (text.charAt(i) == 'a' || text.charAt(i) == 'A')
System.out.print(".-");
if (text.charAt(i) == 'b' || text.charAt(i) == 'B')
System.out.print("-...");
if (text.charAt(i) == 'c' || text.charAt(i) == 'C')
System.out.print("-.-.");
if (text.charAt(i) == 'd' || text.charAt(i) == 'D')
System.out.print("-..");
if (text.charAt(i) == 'e' || text.charAt(i) == 'e')
System.out.print(".");
if (text.charAt(i) == 'f' || text.charAt(i) == 'F')
System.out.print("..-.");
if (text.charAt(i) == 'g' || text.charAt(i) == 'G')
System.out.print("--.");
if (text.charAt(i) == 'h' || text.charAt(i) == 'H')
System.out.print("....");
if (text.charAt(i) == 'i' || text.charAt(i) == 'I')
System.out.print("..");
if (text.charAt(i) == 'j' || text.charAt(i) == 'J')
System.out.print(".---");
if (text.charAt(i) == 'k' || text.charAt(i) == 'K')
System.out.print("-.-");
if (text.charAt(i) == 'l' || text.charAt(i) == 'L')
System.out.print(".-..");
if (text.charAt(i) == 'm' || text.charAt(i) == 'M')
System.out.print("--");
if (text.charAt(i) == 'n' || text.charAt(i) == 'N')
System.out.print("-.");
if (text.charAt(i) == 'o' || text.charAt(i) == 'O')
System.out.print("---");
if (text.charAt(i) == 'p' || text.charAt(i) == 'P')
System.out.print(".--.");
if (text.charAt(i) == 'q' || text.charAt(i) == 'Q')
System.out.print("--.-");
if (text.charAt(i) == 'r' || text.charAt(i) == 'R')
System.out.print(".-.");
if (text.charAt(i) == 's' || text.charAt(i) == 'S')
System.out.print("...");
if (text.charAt(i) == 't' || text.charAt(i) == 'T')
System.out.print("-");
if (text.charAt(i) == 'u' || text.charAt(i) == 'U')
System.out.print("..-");
if (text.charAt(i) == 'v' || text.charAt(i) == 'V')
System.out.print("...-");
if (text.charAt(i) == 'w' || text.charAt(i) == 'W')
System.out.print(".--");
if (text.charAt(i) == 'x' || text.charAt(i) == 'X')
System.out.print("-..-");
if (text.charAt(i) == 'y' || text.charAt(i) == 'Y')
System.out.print("-.--");
if (text.charAt(i) == 'z' || text.charAt(i) == 'Z')
System.out.print("--..");
if (text.charAt(i) == '1')
System.out.print(".----");
if (text.charAt(i) == '2')
System.out.print("..---");
if (text.charAt(i) == '3')
System.out.print("...--");
if (text.charAt(i) == '4')
System.out.print("....-");
if (text.charAt(i) == '5')
System.out.print(".....");
if (text.charAt(i) == '6')
System.out.print("-....");
if (text.charAt(i) == '7')
System.out.print("--...");
if (text.charAt(i) == '8')
System.out.print("---..");
if (text.charAt(i) == '9')
System.out.print("----.");
if (text.charAt(i) == '0')
System.out.print("-----");
if (text.charAt(i) == '-')
System.out.print("...");
if (text.charAt(i) == ' ')
System.out.print(".......");
i++;
}
if (i < text.length()); //Unreachable code
return code;
}
}