添加到字符串数组

时间:2017-10-16 23:30:54

标签: java encapsulation

处理处理封装的程序,无法将用户输入添加到阵列。而且很可能这里还有其他问题。一个是当用户输入选项3时显示输出。此外,我不知道如何告诉用户除非他们删除项目,否则他们不能再添加到包中。在我担心删除之前,我只是想弄清楚添加和显示。

import java.util.ArrayList;
import java.util.Scanner;

class Bag {
    private String[] bag = new String[5];

    public String[] bag() {
        return bag;
    }

    public void add(String bag) {
        for (int i = 0; i < bag.length(); i++) {
            //...
        }
        return;
    }

    public void remove(String bag) {
        return;
    }

    void display() {
        System.out.println("The contents are: " + this.bag());
    }

}

这是第二节课:

import java.util.ArrayList;
import java.util.Scanner;


public class testBag {

    public static void main(String[] args) {
        cart obj = new cart();
        int menu;
        int choice;
        choice = 0;

        Scanner input = new Scanner(System.in);
        ArrayList<testcart> cart = new ArrayList<>();


        System.out.println(" 1. Add item ");
        System.out.println(" 2. Remove item ");
        System.out.println(" 3. Display All");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        while (menu != 4) {

            switch (menu) {
                case 1:
                    while (choice != 2) {
                        System.out.println("What do you want to enter: ");
                        String bag = input.next();
                        obj.add(bag);

                        System.out.println("Enter another? 1: Yes, 2: No");
                        choice = input.nextInt();
                    }
                    break;

                case 2:
                    System.out.println("Enter item to Remove: ");
                    friends.remove(input.next());
                    break;

                case 3:
                    for (int i = 0; i < obj.bag().length; i++) {
                        obj.display();
                    }
                    break;
            }
            System.out.println(" 1. Add item ");
            System.out.println(" 2. Remove item ");
            System.out.println(" 3. Display All items ");
            System.out.println(" 4. Exit ");
            menu = input.nextInt();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您的Bag课程必须有一个计数器,指示它已有多少行李,并将一个新行李存放在相应位置并递增。

  • 要显示您不能直接System.out.println阵列的行李
  • 要删除,您需要遍历所有行李并将其从找到要移除的行李处左移。

Bag课程中实施所有这些:

public class Bag {
    private String[] bag = new String[5];
    private int count = 0; //the new count here

    public void add(String bagToStore) {
        if (count < bag.length){
            bag[count] = bagToStore; //store the new bag in the current position
            count++; //then increment it
        }
    }

    //the remove has more logic because it has to shift the bags if it removes one, 
    //not to leave wholes in the array
    public void remove(String bagToRemove) {
        boolean found = false;
        for (int i=0;i < count; ++i){
            if (bag[i].equals(bagToRemove)){ //to compare Strings you must use equals
                found = true;
            }

            if (found && count < bag.length){
                bag[i] = bag[i+1];
            }
        }

        if (found) count--; 
    }

    void display() {
        for (int i = 0; i < count; ++i) //the display has to be done with a for 
            System.out.println("The contents are: " + bag[i]); 
    }
}

现在还需要调整你的主要课程:

public static void main(String[] args) {
    Bag obj = new Bag();
    int menu, choice = 0;

    Scanner input = new Scanner(System.in);
    do {
        //only print the menu once, you can use a do while for that
        System.out.println(" 1. Add item ");
        System.out.println(" 2. Remove item ");
        System.out.println(" 3. Display All");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        switch (menu) {
        case 1:
            while (choice != 2) {
                System.out.println("What do you want to enter: ");
                obj.add(input.next()); //you call add with input.next as well if you want

                System.out.println("Enter another? 1: Yes, 2: No");
                choice = input.nextInt();
            }
            break;
        case 2:
            System.out.println("What do you want to remove: ");
            obj.remove(input.next()); //just call the remove method on Bag
            break;
        case 3: obj.display(); break; //call the display you already implemented!
        }

    } while (menu != 4);
}

答案 1 :(得分:1)

您实施Bag class

时几乎没有问题
  1. 您已将String元素命名为bad,并将add方法的参数也作为bag存储,因此在add function bag中将其视为String而不是String数组。

  2. 在将元素添加到包中之前,你没有检查包的当前大小,你可以创建一个名为bag的变量并在每次添加元素时递增它,并在删除元素时递减它。

  3. 在显示方法中,您直接打印字符串数组而不是数组元素。

  4. 我通过纠正这些错误更新了你的课程

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Scanner;
    
    class Bag
    {
        private String bag[] = new String[5];
        int size = 0;
    
        Scanner scanner = new Scanner(System.in);
    
        String[] array = new String[2];
    
        public String[] bag(){
            return bag;
        }
    
        public void add(String item)
        {
            if( size < bag.length)
            {
                bag[size] = item;
                size++;
    
            }
            else
            {
                System.out.println("Bag is full remove item before new insertion");
            }
            return;
        }
    
        public void remove(String item)
        {
        }
    
        void display(){
            System.out.println("The contents are: " + Arrays.toString(bag));
    
        }
    
    }