【问题标题】:Why is my code saying that JOptionPane and Interger are unfindable variables?为什么我的代码说 JOptionPane 和 Integer 是无法找到的变量?
【发布时间】:2020-06-25 15:17:28
【问题描述】:

我有一个任务要编写一个程序,该程序使用一个二维数组来显示根据年龄和他们将在那里的天数将您的孩子留在日托中心的费用。我不断收到一条错误消息,上面写着“找不到符号。变量:JOptionPane。”这会显示所有代码,包括该代码。它也显示相同的错误,但这次是“变量:整数”。 我错过了什么?

import java.util.Scanner;

public class DayCare
{
   public static void main(String args[]) 
   {
  // Declare two-dimensional array here.
     double pay[][] = {{30, 60, 88, 115, 140},
                    {26, 52, 70, 96, 120},
                   {24, 46, 67, 89, 110},
                   {22, 40, 60, 75, 88},
                   {20, 35, 50, 66,84}};      

  // Declare other variables.
  int numDays;   
  int age;
  String numDaysString;
  String ageString;
  int QUIT = 99;
  Scanner input = new Scanner(System.in);

  // This is the work done in the getReady() method
  // Perform a priming read to get the age of the child.
  ageString = JOptionPane.showInputDialog("Enter age of child: ");
  age = Interger.parseInt(ageString);

  while(age != QUIT)
  {  
     // This is the work done in the determineRateCharge() method
     // Ask the user to enter the number of days
     numDaysString = JOptionPane.showInputDialog("Enter number of days: ");
     numDays = Interger.parseInt(numDaysString);
        // Print the weekly rate
        System.out.println(" Pay is $" + pay[age][numDays]);

     // Ask the user to enter the next child's age
     ageString = JOptionPane.showInputDialog("Enter age of child: ");
    age = Interger.parseInt(ageString);

  }
  // This is the work done in the finish() method
  System.out.println("End of program");
  System.exit(0);
   } // End of main() method.
} // End of DayCare class.

【问题讨论】:

  • 您没有导入 javax.swing.JOptionPane,并且您拼错了 Integer
  • 哦,哎呀。这是一个关于整数的愚蠢错误。但是导入 javax.swing.JOptionpane 是什么意思?
  • import java.util.Scanner;
  • 导入java.util.Scanner的代码;已经在代码中,我不应该编辑它。我应该使用什么措辞代替 JOptionPane?
  • 整数拼写错误。您没有使用扫描仪,请将其删除。您没有导入 javax.swing.JOptionPane

标签: java swing multidimensional-array


【解决方案1】:

如果您只是学习如何处理二维数组,试试这个,这样您就不必处理烦人的循环和控制台。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class Junk {

public static void main(String [] args) {
    double pay[][] = {{30, 60, 88, 115, 140},
                      {26, 52, 70, 96, 120},
                      {24, 46, 67, 89, 110},
                      {22, 40, 60, 75, 88},
                      {20, 35, 50, 66,84}};      

      final JComboBox<Integer> ageCombo = new JComboBox<Integer>(new Integer[] {0, 1, 2, 3, 4});
      final JComboBox<Integer> daysCombo = new JComboBox<Integer>(new Integer[] {1, 2, 3, 4, 5});
      final JLabel costLabel = new JLabel();
      JDialog dialog = new JDialog();
      dialog.setLayout(new GridBagLayout());
      dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      dialog.add(new JLabel("Age of Child:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 0, 0));
      dialog.add(ageCombo, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 4, 4, 4), 0, 0));
      dialog.add(new JLabel("Number of Days:"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 0, 0));
      dialog.add(daysCombo, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 4, 4, 4), 0, 0));
      dialog.add(costLabel, new GridBagConstraints(0, 2, 2, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 4, 4, 4), 0, 0));
      final String costFmt = " Pay is $%.2f";
      ItemListener listener = new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
              if (e.getStateChange() == ItemEvent.SELECTED) {
                  int age = (Integer) ageCombo.getSelectedItem();
                  int days = (Integer) daysCombo.getSelectedItem() - 1;
                  costLabel.setText(String.format(costFmt, pay[age][days]));
              }
          }
      };
      costLabel.setText(String.format(costFmt, pay[0][0]));
      ageCombo.addItemListener(listener);
      daysCombo.addItemListener(listener);
      dialog.setAlwaysOnTop(true);
      dialog.pack();
      dialog.setLocationRelativeTo(null);
      dialog.setVisible(true);
} // End of main() method.
}

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 2023-04-02
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多