我正在完成一项任务,而且一直没事,但现在我被困了。我们必须制作一个GUI程序,让您选择1个宿舍计划和1个膳食计划。单击“计算”按钮后,每学期的成本将显示在文本字段中。我的ButtonListener有问题。这是我做过的一些事情
private String[] dormList = { "Lincoln Suites Double: $" + LINCOLN_DOUBLE, "Washington Suites Single: $" + WASHINGTON_SINGLE,
"Washington Suites Double: $" + WASHINGTON_DOUBLE, "Washington Suites Triple: $" + WASHINGTON_TRIPLE,
"Roosevelt Suites Double: $" + ROOSEVELT_DOUBLE, "Kennedy Suites Single: $" + KENNEDY_SINGLE };
private String[] mealList = { "10 meals per week plus $200 flex dollars: $" + TEN_MEALS_200_FLEX,
"10 meals per week plus $350 flex dollars: $" + TEN_MEALS_350_FLEX,
"14 meals per week plus $400 flex dollars: $" + FOURTEEN_MEALS_400_FLEX };
我将这些数组制作成组合框。全部大写是我的常数所以我可以随时改变价格。
private class ButtonLister implements ActionListener {
public void actionPerformed(ActionEvent e) {
int total = 0;
String dormSelection = (String) dormsComboBox.getSelectedItem();
String mealSelection = (String) mealsComboBox.getSelectedItem();
total =
}
}
使用$的分隔符进行标记以获取价格并将其添加到总数中是否有意义?或者可能有办法确定它是哪个套件并使用常数来计算总数?
答案 0 :(得分:0)
您可以轻松地将字符串中的钱解析为数字。
Number num = NumberFormat.getCurrencyInstance(Locale.US).parse(str);
修改:为了格式化您的号码,请使用indexof
Number num = NumberFormat.getCurrencyInstance(Locale.US).parse(str.substring(str.indexOf("$")));