java - Swing/awt gcd 计算器
问题描述
我必须在 java swing/awt 中创建一个计算不同函数的计算器。
例如它需要计算 GCD。
首先我创建了 UI,然后是 actionPerformed 函数,但是当我gcd
在 GUI 中执行该函数时,屏幕会冻结。
calculate2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get values from text fields
try {
int a = Integer.parseInt(input1.getText());
int b = Integer.parseInt(input2.getText());
while (a != b) {
if(a > b)
a = a - b;
else
a = b - a;
}
String result1 = "" + (b);
result.setText((result1));
} catch (Exception f) {
JOptionPane.showMessageDialog(rootPane, "ERROR: " + (f.getMessage()));
}
String aField = input1.getText();
String bField = input2.getText();
if (e.getSource() == calculate2) {
if ("".equals(aField) || "".equals(bField)) {
String emptyFieldWarning;
emptyFieldWarning = "One or more fields is/are empty!";
JOptionPane.showMessageDialog(rootPane, emptyFieldWarning);
}
}
}
});
解决方案
在 while 循环中,在 else 分支中,语句应该是b = b - a
推荐阅读
- node.js - 使用 amqplib-mocks 库进行 rabbitMQ 单元测试
- reactjs - React Typescript:排除高阶组件中的属性
- sql-server - 在 SQL 中更改现有表上的文件流文件组
- php - 没有错误,但数据未显示,卡在处理中
- google-sheets-api - Google 表格 v3 API 使用限制
- google-apps-script - 使用 SetValue 写入另一个电子表格失败,没有错误/异常
- python - 无法从 CSV 文件中读取数据并存储在字典中
- typescript - 打字稿:错误枚举无法将未定义转换为对象(枚举未定义)
- regular-language - 使用抽水引理证明常规语言的不规则性 - 错误在哪里
- sqlcl - Sqlcl - 错误消息 = java.library.path 中没有 ocijdbc18