首页 > 解决方案 > 清除内存以创建新项目

问题描述

第五次行动后如何清除记忆?能够再次创建一个新篮子并用新球填充它。

因为现在,当您重新创建篮子并用球填充它时,错误出现在第二个动作(填充篮子)

如何使用第一个存储桶清除缓存以便进入第二个存储桶?

//主要的

public class Dialogue {
private static Scanner scanner = new Scanner(System.in);

private static boolean buildBasket = false;
private static boolean completedBasket = false;
private Basket basket;
private static int volumeOfBasket = 0;

public static void main(String[] args) {
    boolean buildBasket = false;
    boolean completedBasket = false;
    Basket basket =null;
    int volumeOfBasket = 0;
    try {
        while (true) {
            System.out.println("1 - build а basket");
            System.out.println("2 - add balls to basket");
            System.out.println("3 - output information about basket");
            System.out.println("4 - weight of balls in the basket");
            System.out.println("5 - quantity red balls");

            int i = ScunnerInformation.checkInformatio();

            if (i == 1) {
                System.out.println("what max count of ball will into basket?");
                volumeOfBasket = ScunnerInformation.getVolumeBasket();
                Basket newBasket = new Basket("<Basketballs>", volumeOfBasket);
                System.out.println("Basket was build with name: " + newBasket.getNameBasket() +
                        " and with the size " + newBasket.getVolume());
                buildBasket = true;
                basket = newBasket;
            } else if (i == 2) {
                if (buildBasket) {
                    basket = WorkWithBasket.fillBasket(basket);
                    completedBasket = true;
                    System.out.println("you have successfully added balls to the basket");
                }else {
                    System.out.println("error");
                }
                } else if (i == 3) {
                if (completedBasket) {
                    WorkWithBasket.InfoOut(basket);
                } else {
                    System.out.println("error");
                }
            }else if (i == 4) {
                if (completedBasket) {
                    System.out.println("weight basket: " + WorkWithBasket.countWeight(basket));
                } else {
                    System.out.println("error");
                }
            }else if (i==5) {
                if (completedBasket) {
                    System.out.println(WorkWithBasket.countRedBalls(basket) + " it it number of red balls");
                } else {
                    System.out.println("error");
                }
        } else if (i == 6) {
            break;
        }
    }

    }finally {
        if (scanner!=null){
            scanner.close();
        }
    }
}

//填充篮子

public class WorkWithBasket {

public static Basket fillBasket(Basket basket){
    for(int i =0; i < basket.getVolume(); i++){
        Ball temp = new Ball(Color.getRandomColor(), WorkWithBall.CostBall(),WorkWithBall.WeightBall());
        basket.addBall(temp);
    }
    return basket;
}

//添加球

public void addBall(Ball ball){
    if (ball !=null){
        basket[ball.getId() -1] = ball;
    }
}

错误

//篮子初始化

public class Basket {
private Ball[] basket;

public Basket(){
    this.basket = new Ball[this.volume];
}

public Basket (String nameBasket, int volume){
    this.basket = new Ball[volume];
}

public Ball[] getBasket(){
    return basket.clone();
}
public void addBall(Ball ball){
    if (ball !=null){
        basket[ball.getId() -1] = ball;
    }
}

标签: java

解决方案


推荐阅读