這是我正在處理的一項小工作。我是一名初學Java程序員,在這個小程序中遇到了一個問題。出於某種原因,如果數組包含10個元素,則刪除方法不起作用。我很樂意就解決這個問題提供一些建議/幫助。從「完整」陣列中刪除元素
當數組包含10個元素,並且我嘗試刪除一個元素時,textArea中元素的排列方式並非如此。一切都應該改變1,包含9個元素,最後一個元素爲0.當一個元素被刪除時,所有元素都應該調整自己並向左移動。
輸出的默認外觀是:[0,0,0,0,0,0,0,0,0,0]
如果我輸入數字1 - 10,輸出應該是這樣的: [1,2,3,4,5,6,7,8,9,10]
如果用戶試圖刪除最後一個元件,但是將其設置爲0: [1,2,3,4,5,6,7,8,9,0]
H但是,如果用戶試圖刪除另一個元素,比如5,那麼程序會在控制檯中拋出一個超出界限的異常。如果它們除去5,輸出應該是:[1,2,3,4,6,7,8,9,10,0]
更多上下文中,這裏的分配細節:
Create a complete program that has the ability to store and display integer values in an array. The maximum number of values that your program should be able to handle is 10.
- Add additional elements to the array using screen input (textbox and button).
- Remove array elements based on screen input (textbox and button).
- List all of the elements in the array and compute the sum of all the elements.
- List the even elements in the array and compute the sum of the even elements.
- List the odd elements in the array and compute the sum of the odd elements.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class SumElement implements ActionListener {
public static JButton add, remove, sumAll, sumEven, sumOdd;
public static JTextField inputField;
public static JTextArea textArea;
public static int counter = 0;
public static int[] numbers = new int[10];
public static JLabel titleText;
public static void main(String[] args) {
System.out.println("Counter: " + counter);
// Frame
JFrame frame = new JFrame("Integer Sums");
frame.setSize(178, 240);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container panel
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
frame.setContentPane(container);
// Title panel
JPanel titlePane = new JPanel();
titleText = new JLabel("Integer Sums");
// Content panel
JPanel content = new JPanel();
content.setPreferredSize(new Dimension(300, 180));
content.setLayout(null);
// Buttons
add = new JButton("Add");
add.setBounds(4, 50, 80, 20);
add.addActionListener(new SumElement());
remove = new JButton("Remove");
remove.setBounds(88, 50, 80, 20);
remove.addActionListener(new SumElement());
sumAll = new JButton("Sum All");
sumAll.setBounds(4, 74, 164, 20);
sumAll.addActionListener(new SumElement());
sumEven = new JButton("Sum Even");
sumEven.setBounds(4, 98, 164, 20);
sumEven.addActionListener(new SumElement());
sumOdd = new JButton("Sum Odd");
sumOdd.setBounds(4, 122, 164, 20);
sumOdd.addActionListener(new SumElement());
// Text area, and text field
inputField = new JTextField();
inputField.setBounds(4, 21, 164, 25);
JLabel inputLabel = new JLabel("Input integer value below:");
inputLabel.setBounds(12, 6, 164, 13);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(4, 144, 165, 36);
// Adding everything
container.add(titlePane);
titlePane.add(titleText);
container.add(content);
content.add(inputLabel);
content.add(inputField);
content.add(scrollPane);
content.add(add);
content.add(remove);
content.add(sumAll);
content.add(sumEven);
content.add(sumOdd);
// Extras
frame.toFront();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Add")) {
if (counter == 10) {
titleText.setText("Error: too many values");
} else {
numbers[counter] = Integer.parseInt(inputField.getText());
textArea.setText(Arrays.toString(numbers));
counter++;
}
System.out.println("Counter: " + counter);
} else if (event.getActionCommand().equals("Remove")) {
for (int i = 0; i < counter; i++) {
if (Integer.parseInt(inputField.getText()) == numbers[i]) {
for (int x = i; x < counter - 1; x++) {
numbers[x] = numbers[x + 1];
}
textArea.setText(Arrays.toString(numbers));
counter--;
} else {
titleText.setText("Error: value doesn't exist");
}
}
System.out.println("Counter: " + counter);
} else if (event.getActionCommand().equals("Sum All")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
sum += numbers[i];
}
titleText.setText("Sum is " + sum);
} else if (event.getActionCommand().equals("Sum Even")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
if (numbers[i] % 2 == 0) {
sum += numbers[i];
}
}
titleText.setText("Sum of even values is: " + sum);
} else if (event.getActionCommand().equals("Sum Odd")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
if (numbers[i] % 2 != 0) {
sum += numbers[i];
}
}
titleText.setText("Sum of odd values is: " + sum);
}
}
}
下面是代碼修改後的actionPerformed部分。現在刪除最後一個數組值。但是,如果數組已滿,並且用戶試圖刪除最後一個元素以外的元素,則會引發錯誤。我仍然不確定如何解決這個錯誤。
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Add")) {
if (counter == 10) {
titleText.setText("Error: too many values");
} else {
numbers[counter] = Integer.parseInt(inputField.getText());
textArea.setText(Arrays.toString(numbers));
counter++;
}
System.out.println("Counter: " + counter);
} else if (event.getActionCommand().equals("Remove")) {
if (Integer.parseInt(inputField.getText()) == numbers[9]) {
numbers[9] = 0;
counter--;
textArea.setText(Arrays.toString(numbers));
} else {
for (int i = 0; i < counter; i++) {
if (Integer.parseInt(inputField.getText()) == numbers[i]) {
for (int x = i; x <= counter - 1; x++) {
numbers[x] = numbers[x + 1];
}
textArea.setText(Arrays.toString(numbers));
counter--;
}
}
}
System.out.println("Counter: " + counter);
} else if (event.getActionCommand().equals("Sum All")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
sum += numbers[i];
}
titleText.setText("Sum is " + sum);
} else if (event.getActionCommand().equals("Sum Even")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
if (numbers[i] % 2 == 0) {
sum += numbers[i];
}
}
titleText.setText("Sum of even values is: " + sum);
} else if (event.getActionCommand().equals("Sum Odd")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
if (numbers[i] % 2 != 0) {
sum += numbers[i];
}
}
titleText.setText("Sum of odd values is: " + sum);
}
}
}
我認爲反 - 1中爲remove元素的循環是問題。它應該是x <=計數器。將下一個值移動到當前值應該是條件I.e.如果第10個元素被移除,只需將第10個元素賦值爲0,而不是將下一個元素移至當前。 – notionquest
請更具體地說明'它不工作的意義'。你可以做的最好的事情是提供一個'1:輸入集合'(你的數組中有什麼,例如10,9,...,1),'2:你對它做了什麼'(例如「我試圖去除編號3「),'3:預期結果'(例如,我期望數組是......),'4:實際結果' – Ivan
編輯問題,具體細節。 – jxshu