我正在为我的课程完成一项任务,不幸的是我的大脑试图调试最后几行(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 + = visionCOST; ^ 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); ^ JInsurance.java:84:错误:类,接口或枚举预期} ^ 17错误
答案 0 :(得分:1)
使用下面的代码可以解决您的问题
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);
}
}
答案 1 :(得分:0)
首先,将main
方法移出itemStateChanged
方法。