【问题标题】:array list method gives wrong output数组列表方法给出错误的输出
【发布时间】: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


【解决方案1】:

这是您的 Occurs 方法中的错误:

    for (int i = 0; i <= index; i++) {

实际上应该替换为:

    for (int i = 0; i < index; i++) {

因为我们要测试索引下的值,而不是包括索引。


在这一行:

        if (array[i] == a); {

你在错误的地方写错了括号:

        if (array[i] == a) {

这是对您的 List 方法的更正:

public void List() {
    System.out.println("+--------+--------+");
    System.out.println("| Number | Occurs |");
    System.out.println("+--------+--------+");
    List<Integer> alreadyUsed = new ArrayList<Integer>();
    for (int i = 0 ; i < index ; i++){
        if (!alreadyUsed.contains(array[i])){
            System.out.printf("|%8d|%8d|\n", array[i], Occurs(array[i]));
            System.out.println("+--------+--------+");
            alreadyUsed.add(array[i]);
        }       
    }
}

我使用了ArrayList 来知道我已经在数组中遇到了哪个值,这样我就不会错误地计算它们两次。


以下是输出示例:

Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
12
  12 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
46
  46 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
12
  12 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> A
23
  23 is added to the bag. 
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> L
+--------+--------+
| Number | Occurs |
+--------+--------+
|      23|       4|
+--------+--------+
|      12|       2|
+--------+--------+
|      46|       1|
+--------+--------+
Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多