我似乎在 if 语句中的布尔等于错误,并且在removeDie 中我在删除一个骰子时遇到问题

我需要看看如果杯子是空的,printCup 和removeDie 是否返回 false,并从数组或杯子中删除一个骰子,我正在尝试 -- 在removeDie 上,但它没有用我尝试使用 if 与 == 但它给了我错误,因此我切换为等于。关于removeDie,我尝试从数组中获取-1,但它不起作用。我很感谢您对此提出一些建议,提前致谢。


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;

    }



慕娘9325324
浏览 66回答 3
3回答

慕田峪4524236

您的方法removeDie是 type void,这意味着它不返回任何内容。你应该做这样的事情:public boolean removeDie() {&nbsp; &nbsp; boolean ok = true;&nbsp; &nbsp; for (int x = 0; x < dice.size(); x--) {&nbsp; &nbsp; &nbsp; &nbsp; dice.add(new Die());&nbsp; &nbsp; &nbsp; &nbsp; ok = false;&nbsp; &nbsp; }&nbsp; &nbsp; return ok;}进而if(!cup.removeDie()){&nbsp; &nbsp; // ...}

动漫人物

我认为你需要返回一个booleanfrom cup.removeDie(). 也许你可以这样做:public boolean removeDie() {&nbsp; &nbsp; for (int x = 0; x < dice.size(); x--) {&nbsp; &nbsp; &nbsp; &nbsp; return dice.add(new Die());&nbsp; &nbsp; }&nbsp; &nbsp; return false;}但我不确定你的逻辑。

jeck猫

public class IndexDie {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Skapar en tärning och skriver ut den");&nbsp; &nbsp; &nbsp; &nbsp; Die dice1 = new Die();&nbsp; &nbsp; &nbsp; &nbsp; dice1.printDie();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Skapar en kopp med 3 tärningar och skriver ut koppen");&nbsp; &nbsp; &nbsp; &nbsp; Cup cup = new Cup(3);&nbsp; &nbsp; &nbsp; &nbsp; cup.printCup();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("lägger 2 tärningar och skriver ut koppen igen");&nbsp; &nbsp; &nbsp; &nbsp; cup.addDie();&nbsp; &nbsp; &nbsp; &nbsp; cup.addDie();&nbsp; &nbsp; &nbsp; &nbsp; cup.printCup();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Slår alla tärningar i koppen och skriver ut koppen igen,dessutom summan");&nbsp; &nbsp; &nbsp; &nbsp; cup.roll();&nbsp; &nbsp; &nbsp; &nbsp; cup.printCup();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Summan blir: " + cup.sum());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Tar bort 3 tärningar i koppen och skriver ut den");&nbsp; &nbsp; &nbsp; &nbsp; cup.removeDie();&nbsp; &nbsp; &nbsp; &nbsp; cup.removeDie();&nbsp; &nbsp; &nbsp; &nbsp; cup.removeDie();&nbsp; &nbsp; &nbsp; &nbsp; cup.printCup();&nbsp; &nbsp; &nbsp; &nbsp; if (cup.removeDie() == (false)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Koppen är redan tom,finns inget att ta bort");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (cup.removeDie() == (false)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Koppen är redan tom,finns inget att ta bort");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (cup.removeDie() == (false)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Koppen är redan tom,finns inget att ta bort");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (cup.printCup() == (false)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("error tom kopp!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}import java.util.ArrayList;public class Cup {&nbsp; &nbsp; private ArrayList<Die> dice;&nbsp; &nbsp; public Cup(int x) {&nbsp; &nbsp; &nbsp; &nbsp; dice = new ArrayList<Die>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < x; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dice.add(new Die());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void addDie() {&nbsp; &nbsp; &nbsp; &nbsp; dice.add(new Die());&nbsp; &nbsp; }&nbsp; &nbsp; public int sum() {&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < dice.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + dice.get(i).value();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return sum;&nbsp; &nbsp; }&nbsp; &nbsp; public void roll() {&nbsp; &nbsp; &nbsp; &nbsp; for (int p = 0; p < dice.size(); p++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dice.get(p).roll();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public boolean removeDie() {&nbsp; &nbsp; &nbsp; &nbsp; for (int x = dice.size(); x <=1 ; x--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return dice.add(new Die());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean printCup() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Tärning: " + dice);&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}public class Die {&nbsp; &nbsp; private int die;&nbsp; &nbsp; public void roll() {&nbsp; &nbsp; &nbsp; &nbsp; this.die =1 + (int) (Math.random() * 6);&nbsp; &nbsp; }&nbsp; &nbsp; public int value() {&nbsp; &nbsp; &nbsp; &nbsp; return this.die;&nbsp; &nbsp; }&nbsp; &nbsp; public void printDie() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(this.die);&nbsp; &nbsp; }}如果有人对我如何解决问题感兴趣,仍然需要一些更改,但感谢您的帮助
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java