gpt4 book ai didi

java - jComboBox获取所选项目错误

转载 作者:行者123 更新时间:2023-12-01 11:04:48 25 4
gpt4 key购买 nike

嘿,大家好,我在文本框中显示 jcombobox 中的所选项目时遇到了一些问题,这是我的代码:

double Cubao = 0;
double Zambales = 0;
final int Tickets = 50;
jLabel1.setText(String.valueOf(Tickets));
final double discount = .8;
String selecteditem = jComboBox2.getSelectedItem().toString();
String selectitem = jComboBox1.getSelectedItem().toString();
int Passengers = Integer.parseInt(jTextField2.getText());
if (jComboBox2.getSelectedItem().equals("Deluxe AC")) {
if (jComboBox1.getSelectedItem().equals("Cubao_QuezonCity"))
Cubao = 780;
}
if (jComboBox2.getSelectedItem().equals("Ordinary")) {
if (jComboBox1.getSelectedItem().equals("Cubao_QuezonCity"))
Cubao = 500; }
jTextField4.setText(String.valueOf(Cubao * Passengers));

if (jComboBox2.getSelectedItem().equals("Deluxe AC"))
if (jComboBox1.getSelectedItem().equals("Iba_Zambales")) {
Zambales = 530;
if (jComboBox2.getSelectedItem().equals("Ordinary"))
if (jComboBox2.getSelectedItem().equals("Iba_Zambales"))
Zambales = 300;
{

jTextField4.setText(String.valueOf(Zambales * Passengers));
}
{


jTextField4.setEditable(false);
jTextField1.setEditable(false);

现在,当我将总线类型设置为豪华空调时,它将显示以下内容: http://tinypic.com/r/2s9zwcj/8但是当我将总线类型设置为普通时,它不会显示: http://tinypic.com/r/29mu0so/8

我的代码似乎有什么问题?

最佳答案

您多次检查相同的字段,这意味着您有很多嵌套的 if block ,这使得代码难以阅读。我建议这样:

if (jComboBox1.getSelectedItem().equals("Cubao_QuezonCity")) {
if (jComboBox2.getSelectedItem().equals("Deluxe AC")) {
Cubao = 780;
} else {
Cubao = 500;
}
jTextField4.setText(String.valueOf(Cubao * Passengers));
jTextField4.setEditable(false);
jTextField1.setEditable(false);
}
if (jComboBox1.getSelectedItem().equals("Iba_Zambales")) {
if (jComboBox2.getSelectedItem().equals("Deluxe AC")) {
Zambales = 530;
} else {
Zambales = 300;
}
jTextField4.setText(String.valueOf(Zambales * Passengers));
jTextField4.setEditable(false);
jTextField1.setEditable(false);
}

关于java - jComboBox获取所选项目错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com