mardi 15 octobre 2019

I seems to have error with boolean equals in the if-statements and in removeDie I have problem with removing one dice, how to solve these two issues?

I need see if printCup and removeDie return false if the cup is empty and remove one dice from array or the cup, I am trying -- on removeDie but its not useful I tried using if with == but it gave me error, therefore I switched to equals. About the removeDie, I tried for, -1 from the array but it does not work. I appreciate for some advice on this one Thanks in advance.



public class IndexDie {

    public static void main(String[] args) {

        System.out.println("Skapar en tärning och skriver ut den");
        Die dice1 = new Die();
        dice1.printDie();

        System.out.println("Skapar en kopp med 3 tärningar och skriver ut koppen");
        Cup cup = new Cup(3);
        cup.printCup();

        System.out.println("lägger 2 tärningar och skriver ut koppen igen");
        cup.addDie();
        cup.addDie();
        cup.printCup();

        System.out.println("Slår alla tärningar i koppen och skriver ut koppen igen,dessutom summan");
        cup.roll();
        cup.printCup();
        System.out.println("Summan blir: " + cup.sum());

        System.out.println("Tar bort 3 tärningar i koppen och skriver ut den");
        cup.removeDie();
        cup.removeDie();
        cup.removeDie();
        cup.printCup();

        if (cup.removeDie().equals( false) {
            System.out.println("Koppen är redan tom,finns inget att ta bort");
        }
        if (cup.removeDie().equals(false) {
            System.out.println("Koppen är redan tom,finns inget att ta bort");
        }
        if (cup.removeDie().equals( false) {
            System.out.println("Koppen är redan tom,finns inget att ta bort");
        }
        if (cup.printCup().equals( false) {
            System.out.println("error tom kopp!");
        }

    }

}
import java.util.ArrayList;

public class Cup {

    private ArrayList<Die> dice;

    public Cup(int x) {
        dice = new ArrayList<Die>();
        for (int i = 0; i < x; i++) {
            dice.add(new Die());

        }
    }

    public void addDie() {
        dice.add(new Die());

    }

    public int sum() {
        int sum = 0;
        for (int i = 0; i < dice.size(); i++) {
            sum = sum + dice.get(i).value();

        }
        return sum;
    }

    public void roll() {
        for (int p = 0; p < dice.size(); p++) {
            dice.get(p).roll();
        }

    }

    boolean ok = true;

    public void removeDie() {
        for (int x = 0; x < dice.size(); x--) {
            dice.add(new Die());
            ok = false;
        }

    }

    public void printCup() {
        System.out.println("Tärning: " + dice);
        ok = false;
    }

}
public class Die {

    private int die;



    public void roll() {
        this.die =1 + (int) (Math.random() * 6);

    }

    public int value() {
        return this.die;

    }

    public void printDie() {
        System.out.println(this.die);


    }



}

Aucun commentaire:

Enregistrer un commentaire