私はプログラミングに非常に慣れていないため、この宿題で完全に迷っています。割り当ては、2 つの異なるサイコロを表示する GUI を持つことです。ボタンをクリックすると、乱数ジェネレーターはサイコロのさまざまな画像を表示するはずです。「if」ステートメントがないときに画像を表示できるので、パスが機能していることがわかります。「if」ステートメントを追加してサイコロの 1 つに画像を割り当てると、シンボルが見つからないというエラーが表示されます。アイコンが静的に割り当てられたときにこれが機能したため、問題の原因は「if」ステートメントであると思いますが、乱数に戻る可能性があります。私が間違っていることについてアドバイスしてください。添付のコードには、左のダイスの動作しないコードのみが含まれています。
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Random randomNumbers = new Random(); // Generates random numbers
int valLeft; // holds random number
int valRight; // holds random number
// get values for the dice
valLeft = randomNumbers.nextInt(6)+1; // range 1-6
valRight = randomNumbers.nextInt(6)+1; // range 1-6
// assign the image for the left die
if (valLeft==1)
{
ImageIcon leftDie = new ImageIcon("Die1.png");
}
if (valLeft==2)
{
ImageIcon leftDie = new ImageIcon("Die2.png");
}
if (valLeft==3)
{
ImageIcon leftDie = new ImageIcon("Die3.png");
}
if (valLeft==4)
{
ImageIcon leftDie = new ImageIcon("Die4.png");
}
if (valLeft==5)
{
ImageIcon leftDie = new ImageIcon("Die5.png");
}
if (valLeft==6)
{
ImageIcon leftDie = new ImageIcon("Die6.png");
}
// put image on label
imageLabelLeft.setIcon(leftDie);
// assign the image for the right die
ImageIcon rightDie = new ImageIcon("Die6.png");
imageLabelRight.setIcon(rightDie);
// remove the text from the labels
imageLabelLeft.setText(null);
imageLabelRight.setText(null);
// repack the frame for the new images
pack();
}
}