I apologize if people are seeing this again, but I am being a little stubborn and want to see the way I have my code set up to work even if not most efficient. In case new people see this as well, the purpose of this code is to simulate rolling a handful of dice. I am using an arrayList of objects to accomplish this. The issue I am having is as seen below, my object arrayList is printing out each object. However, each object is only able to print out the most recent number rolled by that type of dice.
CODE:
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void printArrayList(ArrayList<VarDice> diceRolls) {
int i;
for(i = 0; i < diceRolls.size(); ++i) {
System.out.print("D" + diceRolls.get(i).getNumSides() + ":");
diceRolls.get(i).printInfo();
System.out.println();
}
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
//Create a new object of VarDice (VarDice dice = new VarDice();)
//Create boolean play that is true
Boolean play = true;
//Create int numSides and int numDice for how many dice are rolled
int numSides;
int numDice;
//Create an int variable for results of the rolls (int results = 0;)
int results = 0;
//Create array list of object VarDice
ArrayList<VarDice> diceRolls = new ArrayList<VarDice>();
while (play) {
//Greet user
System.out.println("Enter the number of dice and number of sides (enter 0 to exit):");
VarDice dice = new VarDice();
try {
//Get user input for numSides
numDice = scnr.nextInt();
//Get user input for numDice
numSides = scnr.nextInt();
if (numDice < 0 || numSides < 0) {
throw new Exception ("Invalid input, both should be a positive number");
}
}
//Create catch block for a inputmismatch (catch (InputMismatchException excpt))
catch (InputMismatchException excpt) {
System.out.println("Invalid input.");
break;
}
//Create catch block for all expectations (catch (Exception excpt)
catch (Exception excpt) {
System.out.println(excpt.getMessage());
break;
}
if (numDice == 0 && numSides == 0) {
play = false;
}
else {
//A for loop is made for number of times a die is rolled according to user input
dice.setNumDice(numDice);
dice.setNumSides(numSides);
for (int i = 0; i < numDice; ++i) {
dice.setRoll(dice.Roll());
dice.printInfo();
results += dice.getRoll();
}
diceRolls.add(dice);
//System.out.println();
}
}
System.out.println();
System.out.println("Results:");
printArrayList(diceRolls);
System.out.println("Total: " + results);
}
}
//Die.java
import java.util.Random;
public class Die {
protected int numRolled;
final Random rand = new Random();
public int getNumRolled() {
return numRolled;
}
public void roll() {
numRolled = rand.nextInt(5) + 1;
}
}
//VarDie.java
import java.util.Scanner;
import java.util.ArrayList;
public class VarDie extends Die {
//Scanner scnr = new Scanner(System.in);
int numSides;
int numDice;
int rolledNumb;
public void setNumSides(int sides) {
numSides = sides;
}
public int getNumSides() {
return numSides;
}
public void setNumDice(int numOfDice) {
numDice = numOfDice;
}
public int getNumDice() {
return numDice;
}
//@Override
public int Roll() {
numRolled = rand.nextInt(numSides) + 1;
return numRolled;
}
public void setRoll(int rolledNum) {
rolledNumb = rolledNum;
}
public int getRoll() {
return rolledNumb;
}
public void printInfo() {
//for (int i = 0; i < numDice; ++i) {
System.out.print(rolledNumb + ", ");
//}
}
}
Example output: Enter the number of dice and number of sides (enter 0 to exit): 12, 3, Enter the number of dice and number of sides (enter 0 to exit): 8, Enter the number of dice and number of sides (enter 0 to exit): 6, 1, 6, Enter the number of dice and number of sides (enter 0 to exit):
Results: D20:3, D10:8, D6:6, Total: 36
My program is properly retrieving the correct amount of rolls, and totaling them up. I just do not know how to give each object all their numbers in my final print. It feels like a simple fix, but I am unsure how to fix this.
Aucun commentaire:
Enregistrer un commentaire