Почему в моем коде написано, что JOptionPane и Interger являются неопределяемыми переменными? - PullRequest
0 голосов
/ 13 марта 2020

У меня есть задание создать программу, которая использует двумерный массив, чтобы показать, сколько будет стоить оставить вашего ребенка в детском саду в зависимости от возраста и сколько дней он там проведет. Я продолжаю получать сообщение об ошибке, которое говорит: «не могу найти символ. Переменная: JOptionPane.» Это отображается для всего кода, включая этот код. Он также отображает ту же ошибку, но на этот раз с «variable: Interger». Чего мне не хватает?

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.

1 Ответ

0 голосов
/ 13 марта 2020

Если вы только учитесь работать с двумерными массивами, попробуйте это, так что вам не придется иметь дело с надоедливыми циклами и консолью.

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.
}
...