问题:让我的代码更有条理
我的尝试:将常用方法纳入方法
问题:当将常用方法放入方法时,程序会中断并且不再有效
我目前正在两个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
}
}
答案 0 :(得分:1)
我不是pro
,但首先,按照惯例,类名应该是大写字母。您可以为int stringLength
和double SLB
使用数组。 drawString
,setColor
和drawRect
这个巨大的列表看起来像一个重复的块,所以循环可以使它更清晰。我认为applet或swing应用程序应该有很长的代码行,因为与其他类相比(虽然它不是强制性的),它提供了用户界面。所以更详细的界面更多的代码行。如果需要,可以将它们分成不同的类
答案 1 :(得分: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
注意:请使用getter“getLengths”而不是字段来重载其他人可能计划的超级课程。