【发布时间】:2016-01-08 23:34:11
【问题描述】:
我有这个作业,我们使用数组来实现包。添加/删除等工作正常,直到我运行列表选项。基本上我应该在包里有一张数字表以及它们出现的次数,但我得到的东西完全不同且不相关。
我应该拥有的:
我实际上得到了什么:
这是那部分代码:
import java.util.Scanner;
public class Bag {
int index = 0;
int[] array = new int[100];
public static void main(String[] args) {
int x = 0;
Bag bag = new Bag();
Scanner scan = new Scanner(System.in);
while (x == 0) {
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char chr = scan.next().charAt(0);
if (chr == 'A') {
int b = scan.nextInt();
bag.Add(b);
}
if (chr == 'D') {
int b = scan.nextInt();
bag.Delete(b);
}
if (chr == 'F') {
int b = scan.nextInt();
bag.Find(b);
}
if (chr == 'S') {
bag.Size();
}
if (chr == 'm') {
bag.Min();
}
if (chr == 'M') {
bag.Max();
}
if (chr == 'L') {
bag.List();
}
if (chr == 'Q') {
x++;
bag.Quit();
}
}
}
public int Occurs(int a) {
int NumberofElements = 0;
for (int i = 0; i <= index; i++) {
if (array[i] == a); {
NumberofElements++;
}
}
return NumberofElements;
}
public void Add(int a) {
array[index] = a;
index++;
System.out.println(" " + a + " is added to the bag. ");
}
public void Delete(int a) {
for (int i = 0; i < index; i++) {
if (a == array[i]) {
while (i < index) {
array[i] = array[i + 1];
i++;
}
System.out.println(" " + a + " is deleted from the bag");
index--;
return;
}
}
System.out.println(" cannot delete " + a + ", does not exist in the bag");
}
public void Find(int a) {
if (Occurs(a) == 0)
System.out.println(" " + a + "does not exist in the bag");
else {
System.out.println(" there is (" + Occurs(a) + " in the bag");
}
}
public void Size() {
System.out.println(" there are " + index + " numbers in the bag" );
}
public void Min() {
int min = array[0];
for (int i = 1; i < index; i++) {
if (min > array[i]) {
min = array[i];
}
}
System.out.println(" the minimum number in the bag is " + min);
}
public void Max() {
int max = array[0];
for (int i = 1; i < index; i++) {
if (max < array[i]) {
max = array[i];
}
}
System.out.println(" the maximum number in the bag is " + max);
}
public void delete (int a) {
for (int i = 0; i < index; i++) {
if (a == array[i]) {
while (i < index) {
array[i] = array[i + 1];
i++;
}
index--;
return;
}
}
}
public void List() {
System.out.println("+--------+--------+");
System.out.println("| Number | Occurs |");
System.out.println("+--------+--------+");
int temp[] = array;
for (int i = 0; i < index; i++) {
if(Occurs(array[i]) == 1) {
System.out.printf("|%8d|%8d|\n", array[i], Occurs (array[i]));
System.out.println("+--------+--------+");
}
else {
System.out.printf("|%8d|%8d|\n", array[i], Occurs(array[i]));
System.out.println("+--------+--------+");
for (int j = 0; j <= Occurs(array[i]); j++) {
delete(array[i]);
}
}
}
array = temp;
}
public void Quit() {
System.out.println("bye!");
}
}
【问题讨论】:
-
什么问题,你没说什么?
-
那部分代码还不够。你需要展示你正在谈论的表格,它是如何声明的,它是如何被操纵的。
Occurs是什么?它看起来像一个方法的名称(但方法的名称不应以大写开头)。它有什么作用?你期望的输出是什么?你得到的“奇怪”输出是什么? -
我添加了屏幕截图并包含了整个代码,对于此次事故,我们深表歉意。
-
stackoverflow.com/help/mcve 请阅读并转发。很难理解代码中发生了什么。您是否尝试调试它?
标签: java arrays list methods arraylist