2013-04-06 126 views
1

我有这个程序,但我有几个问题: 这是我做calcs的地方。莫尔代码使用静态

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 

public class MorseCode { 
public MorseCode() 
    { 
    }//end default constructor 
    public String[] translateHere(String s)throws IOException 
    { 
     String compare = s, codedLine = ""; //userInput toUpperCase 
     int length = compare.length(); //length of userInput 
     String line, file = "C:\\APCS Course B\\Unit 4\\Unit 4 Documents\\morse.txt";// variable holding file name and variable for each letter/number 
     char code; 
     //Constants 
     final int MAX = 36; 
     //Arrays 
     char[] morseLetter = new char[MAX]; 
     String[] morseCode = new String[MAX]; 
     String[] newMessage = new String[length]; 
     //putting user input in a character array; 
     char[] userLetters = compare.toCharArray(); 
     //object creation 
     File openFile = new File(file); 
     Scanner inFile = new Scanner(openFile); 
     int counter = 0; 
     while(inFile.hasNext()) 
      { 
       line = inFile.next(); 
       code = (char)line.charAt(0); 
       //System.out.println(code); 
       morseLetter[counter] = code; 
       morseCode[counter] = inFile.next(); 
       counter++; 
      }//end nested while loop 
     for(int j = 0; j < length; j++) 
     { 
      for(int k = 0; k < MAX; k++) 
      { 
       if(userLetters[j] == morseLetter[k]) 
       { 
        newMessage[j] = morseCode[k]; 
       } 
      }//end nested for loop 
     }//end for loop 
     return newMessage; 
    }//end method that completes translateion 
    public String toString(String a, String[] b) 
{ 
    System.out.println("Input: " + a); 
    System.out.println("Output:"); 
    String output = ""; 
    for(int i = 0; i < b.length; i++) 
    { 
     output = output + b[i]; 
    } 
    return output; 
}//end toString method 
}//end Translate Class 

方式主要有:

import javax.swing.JOptionPane; 
import java.io.*; 
import java.util.Scanner; 
public class MorseCodeTester 
{ 
    public static void main(String[] args)throws IOException 
    { 
     String userInput; 
     final String SENTINEL = "0";//for exiting program when entered 
     //object creation 
     MorseCode text = new MorseCode(); 
     //getting user input to be translated 
     do 
     { 
      Scanner in = new Scanner(System.in); 
      System.out.print("Please enter what you wish to translte to Morse code (no punctuation): "); 
      userInput = in.nextLine(); 
      String compare = userInput.toUpperCase(); 
      String[] codedText = new String[compare.length()]; 
      codedText = text.translateHere(compare); 
      text.toString(userInput, codedText); 
     }while(!userInput.equals(SENTINEL)); 
    }//end main 
}//end class 

所以我的计划是,当我把在输入输出显示即使寿的主要方法我这样做,将其转换为莫尔斯电码的计算黑色。也有任何建议,如果我可以在我的任何方法中使用静态标识符。

这里是文本文件:

1 .---- 

2 ..--- 

3 ...-- 

4 ....- 

5 ..... 

6 -.... 

7 --... 

8 ---.. 

9 ----. 

0 ----- 

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

使用调试器,逐行执行代码。 – 2013-04-06 16:30:35

+0

我试过它仍然无法工作。 – user2059140 2013-04-06 16:32:37

+0

if(userLetters [j] == morseLetter [k]){newMessage [j] = morseCode [k];} - 你在这里打印出来看翻译是否真的在发生? – 2013-04-06 16:37:59

回答

2

您的代码应该使用HashMap致函键莫尔斯电码,这使得你的代码更简单。 然后获取一个字母的莫尔斯码是一个简单的查找。

你也可以加载数据一次,也许在MorseCode的构造函数中。

但是,要解决您的问题,您只是不打印输出。只要返回它,而不是在控制台上显示它。

如果你改变你的toString方法看起来像这样,它应该打印出一些东西。

public String toString(String a, String[] b) { 
    System.out.println("Input: " + a); 
    System.out.print("Output:"); // changed to a print, to avoid newline 
    String output = ""; 
    for (int i = 0; i < b.length; i++) { 
     output = output + b[i]; 
    } 
    System.out.println(output); // this was missing 
    return output; 
}//end toString method 
+0

谢谢你的工作完美! – user2059140 2013-04-06 16:58:02