2014-03-03 69 views
0

问题:让我的代码结构更好如何更好地构建此代码?

我已经试过:把常见的方法进入方法

问题:当把常见的方法进入方法的程序中断,并不再起作用

我目前在两个IDE,Eclipse和IntelliJ之间来回切换。我也使用Sublime来构建小应用程序,所以如果任何答案需要这些IDE中的任何一个,我就全部设置!

在此先感谢您的回复,如果您有任何问题或感觉我缺少某些细节领域,请不要犹豫,问问!

验证码:

package java_assignment; 

// Bradley Duncan 

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

public class textAnalyse extends Applet implements ActionListener { 

    Label textBoxCaption; //Caption for the text box 
    TextField inputField; //Input field 
    Button analyseButton; //Analyse button 
    Button resetButton;  //Reset button 
    String inputText; 
    int stringLength1, stringLength2, stringLength3, stringLength4, stringLength5, stringLength6, stringLength7, stringLength8, stringLength9; //Variables with the string lengths stored for future comparison 
    double MWL, StringCount, SLB1, SLB2, SLB3, SLB4, SLB5, SLB6, SLB7, SLB8, SLB9, SLB11, SLB22, SLB33, SLB44, SLB55, SLB66, SLB77, SLB88, SLB99;//used for working out coordinates 


    public void init() { 

     setSize(450, 350); 

     textBoxCaption = new Label("Please enter your text here:"); 
     inputField = new TextField(35); 
     analyseButton = new Button("Submit"); 
     resetButton = new Button("Restart"); 
     add(textBoxCaption); 
     add(inputField); 
     add(analyseButton); 
     analyseButton.addActionListener(this); 
     add(resetButton); 
     resetButton.addActionListener(this); 
    } 

    public void paint(Graphics g) { 
     g.drawString("BWD", 425, 340); // Puts my signature in the bottom corner 
     inputField.setText(""); 
     inputText = (""); 
     StringCount = 0; 
     g.drawLine(50, 300, 410, 300);  //Drawing the x-axis 
     g.drawLine(50, 300, 50, 50);  //Drawing the y-axis 
     g.setColor(Color.red);       //Setting the bar color 
     g.drawRect(50, (int) SLB11, 40, (int) SLB1); //Drawing the bar 
     g.setColor(Color.black);     //Setting the text color 
     g.drawString("1", 70, 315);     //Giving values to the x-axis 
     g.drawString("" + stringLength1 + "", 40, (int) SLB11);  //Displaying the frequency of the word on the y-axis 
     g.setColor(Color.orange); 
     g.drawRect(90, (int) SLB22, 40, (int) SLB2); //stringLength2 
     g.setColor(Color.black); 
     g.drawString("2", 110, 315); 
     g.drawString("" + stringLength2 + "", 40, (int) SLB22); 
     g.setColor(Color.yellow); 
     g.drawRect(130, (int) SLB33, 40, (int) SLB3); //stringLength3 
     g.setColor(Color.black); 
     g.drawString("3", 150, 315); 
     g.drawString("" + stringLength3 + "", 40, (int) SLB33); 
     g.setColor(Color.green); 
     g.drawRect(170, (int) SLB44, 40, (int) SLB4); //stringLength4 
     g.setColor(Color.black); 
     g.drawString("4", 190, 315); 
     g.drawString("" + stringLength4 + "", 40, (int) SLB44); 
     g.setColor(Color.blue); 
     g.drawRect(210, (int) SLB55, 40, (int) SLB5); //stringLength5 
     g.setColor(Color.black); 
     g.drawString("5", 230, 315); 
     g.drawString("" + stringLength5 + "", 40, (int) SLB55); 
     g.setColor(Color.red); 
     g.drawRect(250, (int) SLB66, 40, (int) SLB6); //stringLength6 
     g.setColor(Color.black); 
     g.drawString("6", 270, 315); 
     g.drawString("" + stringLength6 + "", 40, (int) SLB66); 
     g.setColor(Color.orange); 
     g.drawRect(290, (int) SLB77, 40, (int) SLB7); //stringLength7 
     g.setColor(Color.black); 
     g.drawString("7", 310, 315); 
     g.drawString("" + stringLength7 + "", 40, (int) SLB77); 
     g.setColor(Color.yellow); 
     g.drawRect(330, (int) SLB88, 40, (int) SLB8); //stringLength8 
     g.setColor(Color.black); 
     g.drawString("8", 350, 315); 
     g.drawString("" + stringLength8 + "", 40, (int) SLB88); 
     g.setColor(Color.green); 
     g.drawRect(370, (int) SLB99, 40, (int) SLB9); //stringLength9 
     g.setColor(Color.black); 
     g.drawString("9", 390, 315); 
     g.drawString("" + stringLength9 + "", 40, (int) SLB99); 
     g.drawString("Mean word length is: " + MWL, 200, 330); 
    } 

    public void actionPerformed(ActionEvent e) { 
     inputText = inputField.getText(); //Sets a variable as the inputted text 
     inputText = inputText.replaceAll("\\p{P}", ""); //Removes punctuation from the text 
     for (String retval : inputText.split(" ")) { //Splits the string by spaces 
      StringCount += 1;  //Adds 1 to the string count 
      if (retval.length() == 1)  
       stringLength1 += 1; 
      if (retval.length() == 2) 
       stringLength2 += 1; 
      if (retval.length() == 3) 
       stringLength3 += 1; 
      if (retval.length() == 4) 
       stringLength4 += 1; 
      if (retval.length() == 5) 
       stringLength5 += 1; 
      if (retval.length() == 6) 
       stringLength6 += 1; 
      if (retval.length() == 7) 
       stringLength7 += 1; 
      if (retval.length() == 8) 
       stringLength8 += 1; 
      if (retval.length() == 9) 
       stringLength9 += 1; 
      MWL = ((stringLength9 * 9) + (stringLength8 * 8) + (stringLength7 * 7) + (stringLength6 * 6) + (stringLength5 * 5) + (stringLength4 * 4) + (stringLength3 * 3) + (stringLength2 * 2) + (stringLength1 * 1))/StringCount; 
      //^^^Working out the Mean Word Length 
      SLB1 = ((250/StringCount) * stringLength1); //Working out the coordinates of each bar 
      SLB11 = (300 - SLB1); 
      SLB1 = (300 - SLB11); 

      SLB2 = ((250/StringCount) * stringLength2); 
      SLB22 = (300 - SLB2); 
      SLB2 = (300 - SLB22); 

      SLB3 = ((250/StringCount) * stringLength3); 
      SLB33 = (300 - SLB3); 
      SLB3 = (300 - SLB33); 

      SLB4 = ((250/StringCount) * stringLength4); 
      SLB44 = (300 - SLB4); 
      SLB4 = (300 - SLB44); 

      SLB5 = ((250/StringCount) * stringLength5); 
      SLB55 = (300 - SLB5); 
      SLB5 = (300 - SLB55); 

      SLB6 = ((250/StringCount) * stringLength6); 
      SLB66 = (300 - SLB6); 
      SLB6 = (300 - SLB66); 

      SLB7 = ((250/StringCount) * stringLength7); 
      SLB77 = (300 - SLB7); 
      SLB7 = (300 - SLB77); 

      SLB8 = ((250/StringCount) * stringLength8); 
      SLB88 = (300 - SLB8); 
      SLB8 = (300 - SLB88); 

      SLB9 = ((250/StringCount) * stringLength9); 
      SLB99 = (300 - SLB9); 
      SLB9 = (300 - SLB99); 

     } 
     if (e.getSource() == resetButton) { 
      inputField.setText(""); //Resetting the datafields when reset is pressed. 
      inputText = (""); 
      stringLength1 = 0; 
      stringLength2 = 0; 
      stringLength3 = 0; 
      stringLength4 = 0; 
      stringLength5 = 0; 
      stringLength6 = 0; 
      stringLength7 = 0; 
      stringLength8 = 0; 
      stringLength9 = 0; 
      MWL = 0; 
      StringCount = 0; 
     } 
     repaint(); //Re-painting the applet 
    } 
} 
+5

http://codereview.stackexchange.com/可能是一个更好的地方来问这个问题。 – kkuilla

+3

这听起来像这可能属于http://codereview.stackexchange.com –

+0

我注意到的第一件事是类名称。应该有大写的第一个字母。小写字母用于方法名称。这是Java约定:)。 – anonymous

回答

1

我不是一个pro,但是,类名应该是大写按约定开始。你可以使用你的int stringLengthdouble SLB的数组。 drawStringsetColordrawRect的巨大列表看起来像一个重复的块,因此,循环可以使它更清洁。我认为applet或swing应用程序应该有很长的代码行,因为与其他类相比(尽管它不是强制性的),它提供了用户界面。所以更详细的界面更多的代码行。你可以在单独的类分裂他们,如果你想

+0

+1 Camelcase! :) –

1

好,

,你需要有一个更好的专注于空指针异常。

这应该是一个更好的开始:

public strictfp class TextAnalyse extends Applet implements ActionListener { 

    public static final String UNSET_TEXT = ""; 
    protected Label textBoxCaption = new Label("Please enter your text here:"); // Caption for the 
                       // text box 
    protected TextField inputField = new TextField(35); 
    protected Button analyseButton = new Button("Submit"); 
    protected Button resetButton = new Button("Restart"); 
    protected String inputText = UNSET_TEXT; 
    private java.util.List<Integer> lengths = Collections.EMPTY_LIST; 
    private java.util.List<Integer> slb = Collections.EMPTY_LIST; 
    private java.util.List<Integer> slbCoord = Collections.EMPTY_LIST; 
    protected double MWL; 
    protected double stringCount; 

    { // this is called after constructor-create 
     analyseButton.addActionListener(this); 
     resetButton.addActionListener(this); 
    } 

    public java.util.List<Integer> getLengths() { 
     if (lengths == Collections.EMPTY_LIST) { 
      lengths = new ArrayList<Integer>(9); 
     } 
     return lengths; 
    } 

    public String getInputText() { 
     assert inputText != UNSET_TEXT; // prevent read unset text. 
     return inputText; 
    } 
    .... yourcode 

注:请使用消气“getLengths”,而不是场从超别人超载可能计划另一天。