Java 为什么我的代码说JOptionPane和Interger是不可编译的变量?
我有一项任务是制作一个程序,使用二维数组,根据孩子的年龄和他们在托儿所待的天数,显示让孩子留在托儿所的费用。我一直收到一条错误消息,上面写着“找不到symbol.variable:JOptionPane”。这将显示包括该代码在内的所有代码。它还显示相同的错误,但这次显示的是“变量:整数”。 我错过了什么Java 为什么我的代码说JOptionPane和Interger是不可编译的变量?,java,swing,multidimensional-array,Java,Swing,Multidimensional Array,我有一项任务是制作一个程序,使用二维数组,根据孩子的年龄和他们在托儿所待的天数,显示让孩子留在托儿所的费用。我一直收到一条错误消息,上面写着“找不到symbol.variable:JOptionPane”。这将显示包括该代码在内的所有代码。它还显示相同的错误,但这次显示的是“变量:整数”。 我错过了什么 import java.util.Scanner; public class DayCare { public static void main(String args[]) {
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.
如果您只是在学习如何处理二维阵列,请尝试一下,这样您就不必处理麻烦的循环和控制台
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.
}
导入java.awt.gridbag约束;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ItemEvent;
导入java.awt.event.ItemListener;
导入javax.swing.JComboBox;
导入javax.swing.JDialog;
导入javax.swing.JLabel;
公共类垃圾{
公共静态void main(字符串[]args){
双薪,
{26, 52, 70, 96, 120},
{24, 46, 67, 89, 110},
{22, 40, 60, 75, 88},
{20, 35, 50, 66,84}};
final JComboBox ageCombo=新的JComboBox(新的整数[]{0,1,2,3,4});
final JComboBox daysCombo=新JComboBox(新整数[]{1,2,3,4,5});
最终JLabel costLabel=新JLabel();
JDialog dialog=新建JDialog();
setLayout(新的GridBagLayout());
setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
添加(新JLabel(“儿童年龄:”),新GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,新插入(4,4,4,4),0,0));
添加(ageCombo,新GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,新插入(4,4,4,4),0,0));
添加(新JLabel(“天数:”),新GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,新插入(4,4,4,4),0,0));
添加(daysCombo,新的GridBagConstraints(1,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,新插入(4,4,4,4),0,0));
添加(costLabel,新GridBagConstraints(0,2,2,1,1,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,新插入(4,4,4,4),0,0));
最后一个字符串costFmt=“付款为%.2f”;
ItemListener=新的ItemListener(){
@凌驾
公共无效itemStateChanged(ItemEvent e){
如果(如getStateChange()==ItemEvent.SELECTED){
int age=(整数)ageCombo.getSelectedItem();
int days=(整数)daysCombo.getSelectedItem()-1;
setText(String.format(costFmt,pay[age][days]);
}
}
};
setText(String.format(costFmt,pay[0][0]);
ageCombo.addItemListener(listener);
daysCombo.addItemListener(listener);
dialog.setAlwaysOnTop(true);
dialog.pack();
对话框.setLocationRelativeTo(空);
对话框.setVisible(true);
}//main()方法的结尾。
}
您没有导入javax.swing.JOptionPane,而且您拼错了integerohoops。这是一个关于整数的愚蠢错误。但是导入javax.swing.JOptionpane是什么意思呢?import java.util.Scanner
导入java.util.Scanner的代码;已经在代码中了,我不应该编辑它。我应该用什么措辞来代替JOptionPane?Integer是错误的。您没有使用扫描仪,请将其卸下。您没有导入javax.swing.JOptionPane