Java 理解程序中的变量范围
我是一个编程新手,已经成功地使用这个网站来学习和克服障碍。在我提出我的新问题之前,让我首先感谢所有给予我巨大帮助的人 我正在做我的第一个项目,解决了妨碍程序运行的小问题。然而,这个问题的答案是通过猜测找到的,我非常想知道为什么。这应该是一个可变范围的问题。这是程序的一个片段Java 理解程序中的变量范围,java,variables,scope,Java,Variables,Scope,我是一个编程新手,已经成功地使用这个网站来学习和克服障碍。在我提出我的新问题之前,让我首先感谢所有给予我巨大帮助的人 我正在做我的第一个项目,解决了妨碍程序运行的小问题。然而,这个问题的答案是通过猜测找到的,我非常想知道为什么。这应该是一个可变范围的问题。这是程序的一个片段 public class AlladinLamp { JMenuBar jmBar; JComboBox runners, raceDiv, days, months; JButton horseLi
public class AlladinLamp {
JMenuBar jmBar;
JComboBox runners, raceDiv, days, months;
JButton horseList, ok, clear;
JTextField[] jtxt;
JTextField raceName;
JTextArea message;
String status;
JDialog dialog;
JPanel table, data, jp, middle;
JFrame jfr;
String[] hNam ; // string array holding horse names
int[] hNum, hVal;
int reducedFNH, id, vp, day, month, fnh, dv; // fnh is the number of horses in the
race
Processes processes;
GridBagLayout gbLayout;
GridBagConstraints gbc;
...
public AlladinLamp()
{
...
public ActionListener horseListActionListener = new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
Toolkit myKit = dialog.getToolkit();
Dimension dialSize = myKit.getScreenSize();
dialog.setLocation( dialSize.width/2, dialSize.height/4 );
dialog.setSize( 260, 380 );
dialog.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
jp.setLayout( gbLayout );
jtxt = new JTextField[ fnh ]; <--- array of textfields for the horse names
JLabel label;
String str;
for( int i = 0; i < fnh; i++ )
{
gbc.gridx = 0;
gbc.gridy = i;
str = new Integer( i+1 ) + ".";
label = new JLabel( str );
jp.add( label, gbc );
gbc.gridx = 1;
gbc.gridy = i;
gbc.ipady = 4;
gbc.insets = new Insets(4,0,0,0);
jtxt[i] = new JTextField(15);
jp.add( jtxt[i], gbc );
}
JPanel buttonPanel = new JPanel();
ok = new JButton( "OK" );
ok.addActionListener( okActionListener );
buttonPanel.add( ok );
clear = new JButton ( "CLEAR" );
clear.addActionListener( clearActionListener );
buttonPanel.add( clear );
JScrollPane jscr = new JScrollPane( jp );
dialog.add( jscr, BorderLayout.CENTER );
dialog.add( buttonPanel, BorderLayout.SOUTH );
dialog.setVisible( true );
}
};
公共类AlladinLamp{
JMenuBar-jmBar;
JComboBox跑步者,赛马日,月;
JButton horseList,ok,清除;
JTextField[]jtxt;
JTextField raceName;
JTextArea消息;
字符串状态;
JDialog对话框;
JPanel表,数据,jp,中间;
jfr框架;
String[]hNam;//包含马名的字符串数组
int[]hNum,hVal;
int reducedFNH,id,vp,day,month,fnh,dv;//fnh是该区域的马数
比赛
过程;
网格布局;
gridbaggbc;
...
公共灯
{
...
public ActionListener horseListActionListener=新建ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
Toolkit myKit=dialog.getToolkit();
维度dialSize=myKit.getScreenSize();
dialog.setLocation(dialSize.width/2,dialSize.height/4);
对话框。设置大小(260380);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jp.setLayout(gbLayout);
jtxt=new JTextField[fnh];这与变量作用域无关。您看到的错误意味着jtxt
或hNam
小于i-1
元素,如果超出了它们的界限。如何初始化fnh
?因为您使用它但从不初始化它(至少在这部分代码中。如前所述,在第一个侦听器中初始化包含马名文本字段的数组。jtxt和hnam都不小于程序中所示的i-1元素…?Iffnh>jtxt[]
您将获得ArrayIndexOutOfBounds异常,然后将初始化jText[]
时的fnh
值与启动for
时的fnh
值进行比较
public ActionListener okActionListener = new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
hNam = new String[ fnh ];
hNum = new int[ fnh ];
hVal = new int[ fnh ];
for( int i = 0; i < fnh; i++ )
{
hNam[i] = jtxt[i].getText();
hNum[i] = reduce( i + 1 );
hVal[i] = nameValue( hNam[i] );
}
System.out.println( Arrays.toString(hNam) );
System.out.println( Arrays.toString(hVal) );
setID( hNam );
dialog.setVisible( false );
}
};