我一直收到这个错误消息,说
Guess.java:205: error: illegal start of expression
public String doConversion(String text){
^
Guess.java:205: error: ';' expected
public String doConversion(String text){
^
Guess.java:205: error: ';' expected
public String doConversion(String text){
^
Guess.java:239: error: illegal start of expression
public String doDConversion(String text){
^
Guess.java:239: error: ';' expected
public String doDConversion(String text){
^
Guess.java:239: error: ';' expected
public String doDConversion(String text){
^
Guess.java:274: error: reached end of file while parsing
}
我已经尝试了我能想到的一切,有什么建议吗?
代码在这里:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Guess extends JFrame {
private static int WIDTH = 750;
private static int HEIGHT = 460;
private static int[] count = new int[256];
private int currentNumber;
private int amtOfTries;
private Random myGen;
private int row = 10;
private int col = 20;
private int counter = 0;
private JTextField lineTF = new JTextField(20);
private JTextArea conversionTA;
private JButton exitB, encryptB, decryptB;
private JButton button0, button1, button2, button3, button4, button5;
private JButton startGame;
private ButtonEventHandler eventHandler;
public Guess() {
setTitle("The Shell Game...");
setSize(WIDTH, HEIGHT);
myGen = new Random();
currentNumber = myGen.nextInt(6);
Container pane = getContentPane();
conversionTA = new JTextArea(row, col);
exitB = new JButton("Exit");
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
startGame = new JButton("Start New Game");
eventHandler = new ButtonEventHandler();
exitB.addActionListener(eventHandler);
button0.addActionListener(eventHandler);
button1.addActionListener(eventHandler);
button2.addActionListener(eventHandler);
button3.addActionListener(eventHandler);
button4.addActionListener(eventHandler);
button5.addActionListener(eventHandler);
startGame.addActionListener(eventHandler);
//set the layout of the pane to null
pane.setLayout(null);
//set the locations of the GUI components
//lineTF.setLocation(20, 50);
conversionTA.setLocation(320, 10);
button0.setLocation(20, 100);
button1.setLocation(120, 100);
button2.setLocation(240, 100);
button3.setLocation(20, 50);
button4.setLocation(120, 50);
button5.setLocation(240, 50);
exitB.setLocation(40, 140);
startGame.setLocation(20, 190);
//set the sizes of the GUI components
//lineTF.setSize(200, 30);
conversionTA.setSize(400, 400);
button0.setSize(60, 30);
button1.setSize(60, 30);
button2.setSize(60, 30);
button2.setSize(60, 30);
button3.setSize(60, 30);
button4.setSize(60, 30);
button5.setSize(60, 30);
startGame.setSize(270, 30);
exitB.setSize(200, 30);
//add components to the pane
//pane.add(lineTF);
pane.add(conversionTA);
pane.add(button0);
pane.add(button1);
pane.add(button2);
pane.add(button3);
pane.add(button4);
pane.add(button5);
pane.add(startGame);
pane.add(exitB);
setVisible(true);
conversionTA.setLineWrap(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end of the constructor
private class ButtonEventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("0")) {
//conversionTA.setText("");
if (currentNumber == 0) {
JOptionPane.showMessageDialog(new JFrame(), "Correct Guess! - New Game Started", "Correct Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
String s = "Amount of tries: " + amtOfTries;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(6);
} else {
JOptionPane.showMessageDialog(new JFrame(), "InCorrect Guess! - Try Again", "InCorrect Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
}
}
if (e.getActionCommand().equals("1")) {
if (currentNumber == 1) {
JOptionPane.showMessageDialog(new JFrame(), "Correct Guess! - New Game Started", "Correct Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
String s = "Amount of tries: " + amtOfTries;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(6);
} else {
JOptionPane.showMessageDialog(new JFrame(), "InCorrect Guess! - Try Again", "InCorrect Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
}
}
if (e.getActionCommand().equals("2")) {
if (currentNumber == 2) {
JOptionPane.showMessageDialog(new JFrame(), "Correct Guess! - New Game Started", "Correct Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
String s = "Amount of tries: " + amtOfTries;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(6);
} else {
JOptionPane.showMessageDialog(new JFrame(), "InCorrect Guess! - Try Again", "InCorrect Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
}
if (e.getActionCommand().equals("3")) {
if (currentNumber == 3) {
JOptionPane.showMessageDialog(new JFrame(), "Correct Guess! - New Game Started", "Correct Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
String s = "Amount of tries: " + amtOfTries;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(6);
} else {
JOptionPane.showMessageDialog(new JFrame(), "InCorrect Guess! - Try Again", "InCorrect Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
}
if (e.getActionCommand().equals("4")) {
if (currentNumber == 4) {
JOptionPane.showMessageDialog(new JFrame(), "Correct Guess! - New Game Started", "Correct Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
String s = "Amount of tries: " + amtOfTries;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(6);
} else {
JOptionPane.showMessageDialog(new JFrame(), "InCorrect Guess! - Try Again", "InCorrect Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
}
if (e.getActionCommand().equals("5")) {
if (currentNumber == 5) {
JOptionPane.showMessageDialog(new JFrame(), "Correct Guess! - New Game Started", "Correct Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
String s = "Amount of tries: " + amtOfTries;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(6);
} else {
JOptionPane.showMessageDialog(new JFrame(), "InCorrect Guess! - Try Again", "InCorrect Guess", JOptionPane.ERROR_MESSAGE);
amtOfTries++;
}
}
if (e.getActionCommand().equals("Start New Game")) {
//conversionTA.setText("");
String s = "Amount of tries: " + amtOfTries + " and number was: " + currentNumber;
conversionTA.setText(s);
amtOfTries = 0;
currentNumber = myGen.nextInt(3);
JOptionPane.showMessageDialog(new JFrame(), "New Game Started - Start Guessing", "New Game Started - Start Guessing", JOptionPane.ERROR_MESSAGE);
} else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
}
public String doConversion (String text){
String temp, newText = "";
double inches, conversion;
int y, counter = 0;
char c;
temp = lineTF.getText();
if (temp.length() == 0) {
JOptionPane.showMessageDialog(new JFrame(), "No Input!", "ERROR", JOptionPane.ERROR_MESSAGE);
return " ";
}
newText = "";
for (int x = 0; x < temp.length(); x++) {
c = temp.charAt(x);
y = (int) c;
y = y + 17;
newText += (char) y;
}
/*
for (int x=0; x < count.length; x++) {
newText += " " + (char)x + ":" + count[x];
}
*/
conversionTA.setText("");
return newText;
}
public String doDConversion (String text){
String temp, newText = "";
double inches, conversion;
int y, counter = 0;
char c;
conversionTA.selectAll();
temp = conversionTA.getText();
//JOptionPane.showMessageDialog(new JFrame(), temp, "ERROR", JOptionPane.ERROR_MESSAGE);
if (temp.length() == 0) {
JOptionPane.showMessageDialog(new JFrame(), "No Input!", "ERROR", JOptionPane.ERROR_MESSAGE);
return " ";
}
newText = "";
for (int x = 0; x < temp.length(); x++) {
c = temp.charAt(x);
y = (int) c;
y = y - 17;
newText += (char) y;
}
/*
for (int x=0; x < count.length; x++) {
newText += " " + (char)x + ":" + count[x];
}
*/
conversionTA.setText("");
return newText;
}
}
如果您正确地缩进代码,您会注意到缺少一些括号的问题。如果你使用的是elcipe,点击CTLR+SHIFT+F,然后点击netbeans SHIFT+ALT+F
完成此操作后,您会注意到doConversion
方法声明之前缺少两个右括号。
} // misssing
} // missing
public String doConversion(String text) {
还有一个在最后一页。