Если бы кто-нибудь мог подтолкнуть меня в правильном направлении, это было бы здорово.
Или скажите, нужно ли мне переделывать кусок или что-то в этом роде.
Это программа для игры в кости. Я пытаюсь сделать так, чтобы каждый отдельный ролик отображался в eDieField.
вот класс:
import java.util.Random;
public class dice
{
private int times;
private int roll;
private int side;
public int[] each;
Random roller = new Random();
public void setTimes(int rolls)
{
times = rolls;
}
public void setSides(int die)
{
side = die;
}
public int getRoll()
{
int[] each = new int[times];
int total = 0;
int c = 0;
int i = 0;
while (c < times)
{
c = c + 1;
roll = roller.nextInt(side);
roll = roll + 1;
each[i] = roll;
total = total + roll;
System.out.print(each[i] + " ");
i = i + 1;
}
return total;
}
public int getEach()
{
return each[/*each number in the array*/];
}
}
вот GUIWindow:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIWindow extends JFrame
{
private dice dices = new dice();
private JLabel dice = new JLabel("# of Dice");
private JLabel sides = new JLabel("# of Sides");
private JLabel total = new JLabel("Total:");
private JTextField diceField = new JTextField("");
private JTextField sideField = new JTextField("");
private JTextField totField = new JTextField("");
private JButton button = new JButton ("Roll!");
private JTextField eDieField = new JTextField("");
private JLabel empt = new JLabel("Here is each roll");
// Constructor
public GUIWindow()
{
JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
dataPanel.add(dice);
dataPanel.add(sides);
dataPanel.add(diceField);
dataPanel.add(sideField);
JPanel rPanel = new JPanel(new GridLayout(1, 3, 12, 6));
rPanel.add(button);
rPanel.add(total);
rPanel.add(totField);
JPanel eDiePan = new JPanel(new GridLayout(2, 1, 12, 6));
eDiePan.add(empt);
eDiePan.add(eDieField);
Container container = getContentPane();
container.add(dataPanel, BorderLayout.WEST);
container.add(rPanel, BorderLayout.EAST);
container.add(eDiePan, BorderLayout.SOUTH);
button.addActionListener(new dieListener());
}
// >>>>>>> The controller <<<<<<<<
private class dieListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
String input = diceField.getText();
int die = Integer.parseInt(input);
dices.setTimes(die);
String inputa = sideField.getText();
int side = Integer.parseInt(inputa);
dices.setSides(side);
int tot = dices.getRoll();
totField.setText("" + tot);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(GUIWindow.this,
"Sorry,\nyou can do that with dice.",
"Dice Fail",
JOptionPane.ERROR_MESSAGE);
}
int eachd = dices.getEach();
eDieField.setText("" + eachd);
}
}
}
и основной:
import javax.swing.*;
public class diceRoller
{
public static void main(String[] args)
{
GUIWindow theGUI = new GUIWindow();
theGUI.setTitle("Dice Roller");
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.pack();
theGUI.setVisible(true);
}
}
Я надеюсь, что это не плохое поведение или что-то еще, я просто не знаю, что делать. (А мой тупой учебник бесполезен) Скажем, вы вводите 4 кубика с 4-мя сторонами и числа, которые были бы включены, если вы бросили их вручную, были 2, 4, 3, 4. добавлено, что будет 13. и 2 4 3 4 перейти в «eDieField». Я не могу заставить это работать. Я продолжаю получать nullPointerException и не знаю, как сохранить массив каждый [], чтобы я мог получить числа для eDieField (или что-то еще, например, список или что-то еще).