0

I am very new to java and I set a goal for myself to make a dice rolling program (keeping it small). The end goal is to be able to roll a user-selected amount of dice and be able to have each die have a different amount of side if need be and I have it get the number of dice and how many sides each has. This is the code I made for it (Might be really bad, sorry if it is) :

public class Roller {
 public final Random rando;
 public final int faces;
 public Roller(int faces) {
 this.rando = new Random();
 this.faces = faces;
 }
 public int roll() {
 return 1 + rando.nextInt(faces);
 }
 //above code is not mine I built off what my friend wrote cause i didnt know if i still need it
 public static void main(String[] args) {
 Random rand = new Random();
 Scanner scan = new Scanner(System.in);
 System.out.print("How many dice do you want to roll?\n");
 int D6 = scan.nextInt();
 ArrayList<Integer> list = new ArrayList<>();
 for (int i = 0; i < D6; i++) {
 System.out.print("How many sides does die " + (i + 1) + " have?\n");
 Roller dice = new Roller(scan.nextInt());
 list.add(dice.roll());
 }
 }
}

Now I'm at the point where I want to display the ArrayList but I want to display it as

"Dice 1 rolled #

Dice 2 rolled #"

etc. and I'm lost on how to do that especially with the varying number of dice. Any help is very appreciated.

4
  • I suggest that you use another for loop similar to the one you already have. Commented Jul 4, 2017 at 0:26
  • 2
    I also suggest that you learn about Java naming conventions. Classes generally start with an uppercase letter and variable names with a lower case letter. Next use more descriptive names. For example, D6, should be something like diceCount. Commented Jul 4, 2017 at 0:27
  • If D6 is a reference to D&D, it could be much more descriptive than diceCount, but that's very situational and context based. Using it as a variable name for a scanner input indicates that this probably isn't the case. Commented Jul 4, 2017 at 0:32
  • the D6 was left over from a very bad program that came before this so I scrapped it but I kept some of the code since it still was needed and I guess I forgot to rename it. Also, thanks for the tip, @Code-Apprentice, I'll keep that in mind Commented Jul 4, 2017 at 2:41

1 Answer 1

1

Let's assume you ran this and now have a List of values, [1, 3, 5, 2, 4] and you want to display them as you described.

In your main method, you have the list, so you could do some looping and string formatting to get your desired output. (edited to use printf() rather than String.format())

// in main...
// after list has all it's values
for (int i = 0; i < list.size(); i++) {
 System.out.printf("Dice #%d rolled %d", i+1, list.get(i));
}

Note that the below statements are still valid, and can still be applied to printf(...)

To walk through it, String formatting is just a fancy way to format your strings (funny how that works out). The first %d corresponds to the first value given to format(), which is i+1. It's i+1 as opposed to plain i because otherwise you'd see "Dice #0 rolled ..." first, since you start indexing arrays and lists at 0. With the second %d in the format() call, you pass in list.get(i) which is the value in the list at the given index. This should correspond nicely to the order of the rolls.

This didn't have to be done with String formatting. I find it tends to be better and easier to read personally, but it is easily substituted with String concatenation.

//replace the print statement with this if you want
System.out.println("Dice #" + (i+1) + " rolled " + list.get(i));

It seems sloppier to me IMO, and needing to remember to leave spaces, or omit spaces between concatenated parts can be annoying.

answered Jul 4, 2017 at 0:22
Sign up to request clarification or add additional context in comments.

4 Comments

Note that Dice is a reference variable with type Roller. The roll() method is in the code shown in the OP.
Man, when people capitalize their variable names, I never notice what's going on. Me and my feeble eyes.
Next, I suggest using printf() instead of println(String.format()). Much less typing.
Bah I never remember to use printf(). I don't usually ever use System.out anymore, since I just use loggers with log.info(...) now. I'm getting rusty with this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.