One of the classes I am currently working on is a DiceRoll project. One of the requirements of the project is to use nested if else statements and at least one switch
statement. Another requirement is to give the slang for the dice roll as per this image Dice Roll Slang
Here is my current code, with an explanation at the bottom:
import java.util.Random;
public class Dice {
public int die1;
public int die2;
public int total;
public String name;
public boolean doubles = false;
Random random = new Random();
public void setDie1() {
die1 = random.nextInt(6) + 1;
}
public void setDie2() {
die2 = random.nextInt(6) + 1;
}
public void setTotal() {
total = die1 + die2;
}
public int getDie1() {
return "Roll #1: " + die1;
}
public int getDie2() {
return "Roll #2: " + die2;
}
public int getTotal() {
return die1 + "plus" + die2 + "=" + total;
}
public boolean checkDouble() {
if (die1 == die2) {
doubles = true;
return " ";
} else {
doubles = false;
return " ";
}
return " ";
}
public String getName() {
if (doubles = true){
name = "1";
} else if (total = 7) {
name = "7";
} else if (total = 5) {
name = "5";
} else if (total = 9) {
name = "9";
} else if (total = 11) {
name = "11";
} else if ((die1 = 1 && die2 = 2) || (die1 = 2 && die2 = 1)) {
name = "2";
} else if ((die1 = 4 && die2 = 2) || (die1 = 2 && die2 = 4)) {
name = "6";
}
}
}
So my original idea was to create nested if else statements in getName. These would correspond to numbers and then I would use these numbers to make a switch statement - also in getName - that would return the dice slang depending on what number was rolled. The only problem I found with this is that since there are multiple ways to roll each number.
If the number is a six for example, you can roll a double 3, or a 2 and a 4. That means I will have to create a lot of different cases and create a very long switch statement. I also end up getting an error with the name = "2";
and name = "6";
statements where it is expecting a <=
in place of the =
for the conditions after the &&
.
I think that there is definitely a better way to do this, but I just cannot figure it out. I am going to keep trying and working at it, but I greatly appreciate any help. Thank you.
Aucun commentaire:
Enregistrer un commentaire