Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调试问题,不确定如何修复_Java - Fatal编程技术网

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

Java 调试问题,不确定如何修复,java,Java,我正在为我的班级做一项作业,不幸的是,在调试最后几行(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;

我正在为我的班级做一项作业,不幸的是,在调试最后几行(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);
      }
}
}

这是我的错误

java:69:错误:类型的开头非法 其他的 ^java:69:错误:';'预期 其他的 ^java:70:错误:类型的非法开始 如果(选择==ItemEvent.SELECTED) ^java:70:错误:';'预期 如果(选择==ItemEvent.SELECTED) ^java:70:错误:应为 如果(选择==ItemEvent.SELECTED) ^java:71:错误:应为 grandTotal+=visionCOST; ^java:72:错误:类型的开始非法 其他的 ^java:72:错误:';'预期 其他的 ^java:73:错误:类型的非法开始 grandTotal-=visionCOST; ^java:74:错误:应为 totalCost.setText(“$”+grandTotal); ^java:74:错误:类型的开始非法 totalCost.setText(“$”+grandTotal); ^java:74:错误:“)”应为 totalCost.setText(“$”+grandTotal); ^java:74:error:';'预期 totalCost.setText(“$”+grandTotal); ^java:74:错误:类型的开始非法 totalCost.setText(“$”+grandTotal); ^java:74:错误:应为 totalCost.setText(“$”+grandTotal); ^java:74:error:';'预期 totalCost.setText(“$”+grandTotal); ^java:84:错误:类、接口或枚举应为}^17个错误


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

使用下面的代码,它将解决您的问题

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);
      }
}

您是否试图在另一个函数中定义公共静态void main(字符串[]args)?检查上一个函数的右括号在哪里。看起来你的括号太多了。使用IDE查找匹配的大括号<代码>因此不是调试服务。由于输入错误而关闭…将代码格式化为人类可读的,作为人类,您可能会更成功地阅读它。