我正在使用GUI构建语音算术求解器。我让它运行并且正在触发ActionListener。但是,我做错了,因为如果我把send + more = money,它应该解决这个难题并输出这些字符串的数字版本。无论如何我都得到了另一个条件,哪个输出“不能用这些词语”。我无法弄清楚我应该发送回ActionListener以满足适当的条件。昨晚我决定添加一个GUI之前的代码工作正常,所以我确定这只是我没有把正确的东西放进去ClickListener方法。请帮忙!
import java.util.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SummationSolver extends JFrame
{
private JButton button;
private JLabel label1;
private JLabel label2;
private JTextField textField;
private static final int FIELD_WIDTH = 15;
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 400;
String input;
public SummationSolver()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createComponents()
{
Color tennesseeOrange = new Color(255, 165, 0);
button = new JButton("Generate");
button.setForeground(Color.BLACK);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
label1 = new JLabel("Enter input: ");
label1.setForeground(Color.BLACK);
textField = new JTextField(FIELD_WIDTH);
label2 = new JLabel("");
label2.setForeground(Color.BLACK);
JPanel panel = new JPanel();
panel.setBackground(tennesseeOrange);
panel.add(label1);
panel.add(textField);
panel.add(button);
panel.add(label2);
add(panel);
}
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
input = (textField.getText());
solve(input);
if (input.equals(""))
{
label2.setText("word");
}
else
{
label2.setText("Does not work with this set of words");
}
}
}
static int eval(String word)
{
int val = 0;
StringTokenizer str = new StringTokenizer(word, "*/+-", true);
while (str.hasMoreTokens())
{
String next = str.nextToken().trim();
if (next.equals("+"))
{
val += Integer.parseInt(str.nextToken().trim());
}
else if (next.equals("-"))
{
val -= Integer.parseInt(str.nextToken().trim());
}
else if (next.equals("*"))
{
val *= Integer.parseInt(str.nextToken().trim());
}
else if (next.equals("/"))
{
val /= Integer.parseInt(str.nextToken().trim());
}
else
{
val = Integer.parseInt(next);
}
}
return val;
}
static String solve(String word)
{
char c = 0;
for (int i = 0; i < word.length(); ++i)
{
if (Character.isAlphabetic(word.charAt(i)))
{
c = word.charAt(i);
break;
}
}
if (c == 0)
{
String[] ops = word.split("=");
int o1 = eval(ops[0]), o2 = eval(ops[1]);
if (o1 == o2) return word;
else return "";
}
else
{
char[] dset = new char[10];
for (int i = 0; i < word.length(); ++i)
if (Character.isDigit(word.charAt(i)))
dset[word.charAt(i)-'0'] = 1;
for (int i = 0; i < 10; ++i)
{
if (dset[i] == 0)
{
String r = solve(word.replaceAll(String.valueOf(c),
String.valueOf(i)));
if (!r.isEmpty()) return r;
}
}
}
return "";
}
public static void main(String[] args)
{
JFrame frame = new SummationSolver();
frame.setTitle("Summation Solver");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
答案 0 :(得分:1)
似乎您需要在input = solve(input);
方法中添加actionPerformed()
。详细了解String
immutability
答案 1 :(得分:-1)
也许您可能想要查看此存储库:它是使用JavaFX解决语言算术问题的解决方案。 Firefly algorithm GitHub
关于存储库:
例如,单元测试
import com.core.luciernaga.Programa;
import com.core.luciernaga.Luciernaga;
import com.core.luciernaga.Util;
@Test
public void first(){
Programa p = new Programa();
p.setOperacion("+");
Util.setEnableInfo(true);
p.setPalabra1("MONEY".toCharArray());
p.setPalabra2("MORE".toCharArray());
p.setPalabra3("SEND".toCharArray());
int n = 0;
int totalIterations = 30;
while(p.getLuciernagaSolucion() == null && n < totalIterations){
p.programa();
n++;
}
if (p.getLuciernagaSolucion() != null) {
Util.info("Problem solved!");
p.mostrarSolución();
}
}