调试问题,不确定如何修复



我正在为我的班级做作业,不幸的是,我在调试最后几行(69-84)时脑子进水了。如果有人能帮我一把,那就太好了!

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*;
public class JInsurance extends JFrame implements ItemListener 
{
final int hmoCOST = 200;
final int ppoCOST = 600;
final int dentalCOST = 75;
final int visionCOST = 20;
int totalCost = 0;
int grandTotal = totalCost;
JCheckBox hmoBox = new JCheckBox("HMO cost per month is $" + hmoCOST, false);
JCheckBox ppoBox = new JCheckBox("PPO cost per month is $" + ppoCOST, false);
JCheckBox dentalBox = new JCheckBox("Dental coverage is an additional $" + dentalCOST, false);
JCheckBox visionBox = new JCheckBox("Vision coverage is an additional $" + visionCOST, false);
JLabel selectLabel = new JLabel("Select additional coverage");
JLabel selectPrice = new JLabel("The cost for your additional coverage is ");
JTextField totalPrice = new JTextField(5);
JLabel optionExplainLabel = new JLabel("The total cost is $ " + totalCost + ".");
JLabel optionExplainLabel2 = new JLabel("Check the options you want.");
public JInsurance()
{
super("Insurance Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(selectLabel);
add(optionExplainLabel);
add(optionExplainLabel2);
add(hmoBox);
add(ppoBox);
add(dentalBox);
add(visionBox);
add(selectLabel);
add(totalPrice);
totalPrice.setText("$" + totalCost);
hmoBox.addItemListener(this);
ppoBox.addItemListener(this); 
dentalBox.addItemListener(this);
visionBox.addItemListener(this);
}
public void itemStateChanged(ItemEvent event)
{
Object source = event.getSource();
int select = event.getStateChange();
if(source == hmoBox)
if(select == ItemEvent.SELECTED)
grandTotal += hmoCOST;
else
grandTotal -= hmoCOST;
else if(source == ppoBox)
{
if(select == ItemEvent.SELECTED)
grandTotal += ppoCOST;
else
grandTotal -= ppoCOST;
}
else 
if(select == ItemEvent.SELECTED)
grandTotal += dentalCOST;
else
grandTotal -= dentalCOST;
}
else
if(select == ItemEvent.SELECTED)
grandTotal += visionCOST;
else
grandTotal -= visionCOST;
totalCost.setText("$" + grandTotal);
public static void main(String[] args)
{
JInsurance aFrame = new JInsurance();
final int WIDTH = 450;
final int HEIGHT = 400;
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
}
}
}

这是我的错误

JInsurance.java:69:错误:类型的非法启动其他的^JInsurance.java:69:错误:";"预期其他的^JInsurance.java:70:错误:类型的非法启动if(select==ItemEvent.SELECTED)^JInsurance.java:70:错误:";"预期if(select==ItemEvent.SELECTED)^JInsurance.java:70:错误:应为if(select==ItemEvent.SELECTED)^JInsurance.java:71:错误:应为grandTotal+=愿景成本;^JInsurance.java:72:错误:类型的非法启动其他的^JInsurance.java:72:错误:";"预期其他的^JInsurance.java:73:错误:类型的非法启动grandTotal-=visionCOST;^JInsurance.java:74:错误:应为totalCost.setText("$"+grandTotal);^JInsurance.java:74:错误:类型的非法启动totalCost.setText("$"+grandTotal);^JInsurance.java:74:错误:应为")"totalCost.setText("$"+grandTotal);^JInsurance.java:74:错误:";"预期totalCost.setText("$"+grandTotal);^JInsurance.java:74:错误:类型的非法启动totalCost.setText("$"+grandTotal);^JInsurance.java:74:错误:应为totalCost.setText("$"+grandTotal);^JInsurance.java:74:错误:";"预期totalCost.setText("$"+grandTotal);^java:84:错误:类、接口或枚举应为}^17个错误

使用下面的代码,它将修复您的问题

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*;
public class JInsurance extends JFrame implements ItemListener 
{
final int hmoCOST = 200;
final int ppoCOST = 600;
final int dentalCOST = 75;
final int visionCOST = 20;
int totalCost = 0;
int grandTotal = totalCost;
JCheckBox hmoBox = new JCheckBox("HMO cost per month is $" + hmoCOST, false);
JCheckBox ppoBox = new JCheckBox("PPO cost per month is $" + ppoCOST, false);
JCheckBox dentalBox = new JCheckBox("Dental coverage is an additional $" + dentalCOST, false);
JCheckBox visionBox = new JCheckBox("Vision coverage is an additional $" + visionCOST, false);
JLabel selectLabel = new JLabel("Select additional coverage");
JLabel selectPrice = new JLabel("The cost for your additional coverage is ");
JTextField totalPrice = new JTextField(5);
JLabel optionExplainLabel = new JLabel("The total cost is $ " + totalCost + ".");
JLabel optionExplainLabel2 = new JLabel("Check the options you want.");
public JInsurance()
{
super("Insurance Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(selectLabel);
add(optionExplainLabel);
add(optionExplainLabel2);
add(hmoBox);
add(ppoBox);
add(dentalBox);
add(visionBox);
add(selectLabel);
add(totalPrice);
totalPrice.setText("$" + totalCost);
hmoBox.addItemListener(this);
ppoBox.addItemListener(this); 
dentalBox.addItemListener(this);
visionBox.addItemListener(this);
}
public void itemStateChanged(ItemEvent event)
{
Object source = event.getSource();
int select = event.getStateChange();
if(source == hmoBox)
if(select == ItemEvent.SELECTED)
grandTotal += hmoCOST;
else
grandTotal -= hmoCOST;
else if(source == ppoBox)
{
if(select == ItemEvent.SELECTED)
grandTotal += ppoCOST;
else
grandTotal -= ppoCOST;
}
else 
if(select == ItemEvent.SELECTED)
grandTotal += dentalCOST;
else
grandTotal -= dentalCOST;
}
else
if(select == ItemEvent.SELECTED)
grandTotal += visionCOST;
else
grandTotal -= visionCOST;
totalCost.setText("$" + grandTotal);
}      
public static void main(String[] args)
{
JInsurance aFrame = new JInsurance();
final int WIDTH = 450;
final int HEIGHT = 400;
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
}
}

首先,将main方法从itemStateChanged方法中移出。

相关内容

最新更新