3 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
您的方法removeDie是 type void,這意味著它不返回任何內(nèi)容。
你應(yīng)該做這樣的事情:
public boolean removeDie() {
boolean ok = true;
for (int x = 0; x < dice.size(); x--) {
dice.add(new Die());
ok = false;
}
return ok;
}
進(jìn)而
if(!cup.removeDie()){
// ...
}

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
我認(rèn)為你需要返回一個(gè)booleanfrom cup.removeDie(). 也許你可以這樣做:
public boolean removeDie() {
for (int x = 0; x < dice.size(); x--) {
return dice.add(new Die());
}
return false;
}
但我不確定你的邏輯。

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
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() == (false)) {
System.out.println("Koppen ?r redan tom,finns inget att ta bort");
}
if (cup.removeDie() == (false)) {
System.out.println("Koppen ?r redan tom,finns inget att ta bort");
}
if (cup.removeDie() == (false)) {
System.out.println("Koppen ?r redan tom,finns inget att ta bort");
}
if (cup.printCup() == (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();
}
}
public boolean removeDie() {
for (int x = dice.size(); x <=1 ; x--) {
return dice.add(new Die());
}
return false;
}
public boolean printCup() {
System.out.println("T?rning: " + dice);
return 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);
}
}
如果有人對(duì)我如何解決問(wèn)題感興趣,仍然需要一些更改,但感謝您的幫助
添加回答
舉報(bào)