0

For a school assignment I need to work with do/while/for loops(which suits the best) with randomized dice throws. The program stops once it throws a 6. But the output needs to look like the input char I give. I know that I need to use system.out.printf to format the output. It stops once the dice throws 6 characters. Would be great if someone could explain how and why too, so I can learn it for the next time.

Here's an example output:

# #
 #
# #

# #
 #
# #

#
 #
  #

# #
# #
# #

Code I have so far(also experimenting right now):

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("What character do you want to use: ");

        String character = input.nextLine();

        char character2 = character.charAt(0);

        do{
            //math random probably needs to be put here?

            System.out.printf("%1s %2s\n",character2, character2); //formatting of output, not yet finished


        } while();
        //not sure if do while is better in this situation. For loops possible too?

    }
}
perissf
  • 15,979
  • 14
  • 80
  • 117
N3ur0
  • 47
  • 7
  • Hello. To format code properly, select it with your mouse and press the `{}` button. I've gone ahead and done that so that all the code shows up together. Also, please paste text rather than screenshotting it. – Chai T. Rex Sep 28 '18 at 13:22
  • @NicholasK I think they're looking for how to use `Math.random()` in the output rather than handle user input. – Chai T. Rex Sep 28 '18 at 13:27
  • that's a dice game using numbers. There are many examples of that on the internet, however I just can't figure out how to randomize this whole code. – N3ur0 Sep 28 '18 at 13:29
  • Hi, unsure exactly what you want. Basically you put an input character in, and you then want to roll the dice until it is a six, and then finish the program? – DubDub Sep 28 '18 at 13:34
  • 1
    @Pluto477, please refine a question a little more. Do you have problem with using `Math.random()`? Or with displaying a character? Or formatting output to look like a face of a die? What precisely is the problem you're having? Note that answering works best if there's one problem you have, for multiple problems you should ask multiple specific questions. – M. Prokhorov Sep 28 '18 at 13:35
  • @M.Prokhorov and everyone else, the program I want is that it randomized dice throws outputting the ASCII character I input(for example: #). And the output is demonstrated as above. it will keep rolling untill it hits 6 (outputted in the input character which is #) – N3ur0 Sep 28 '18 at 13:42
  • @Pluto477, but then that's a question where (assuming attached code is all you have) you are asking us to write a program for you. Better approach here is to break the assignment down to steps, each with increasing likeness of what you're trying to achieve. For instance, you seem to already done the outputting an ASCII character part. Then, try outputting the character and on the same line some random number, say, ten times. Then make this random number to be `[1, 6]`, still output it ten times. Then, make it so it outputs until the number is `6`. Lastly, output a formatted die face. – M. Prokhorov Sep 28 '18 at 13:50
  • Considering the above, you can ask whenever you don't understand some step of what I've described. (But remember, many of the questions already been asked and answered, so use the Site's search functions before committing to asking). – M. Prokhorov Sep 28 '18 at 13:52
  • @M.Prokhorov Last thing I want is someone to make the program for me, but it's more that I need an example of how I can convert the random number to be outputted in the formatted characters. I now know how the dice works using numbers, and it stops on number 6. But I don't know how to convert that to the formatted printf – N3ur0 Sep 28 '18 at 13:56
  • Ah, I see. You should've said it from the start that you're more interested in how to print the die. I think the accepted answer has what you need. You might also want to read [this pretty long document](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html) about how `String.format` does its things (all of this can be used for various outputting techniques). – M. Prokhorov Sep 28 '18 at 14:05

3 Answers3

2

This is the basic flow that you need to follow:

do
  roll = random number between 1 and 6
  print the face of dice showing value of roll using selected character
while roll not 6

In Java you can use the nextInt(n) method of the Random class to get random numbers. One wrinkle is that it returns a number between 0 and n-1 inclusive, so you'll need to add 1 to the value returned, as in:

int roll = 1 + rand.nextInt(6);

As for printing the faces I would define each face this way, e.g. for 5:

String f5 = "x x\n x \nx x";

Then replace x with the character selected, ch, using f5.replace('x', ch).

You could even use an array to hold the faces, as in:

String[] faces = new String[6];
faces[0] = "  \n x \n   ";
<snip>
faces[6] = "x x\nx x\nx x";

Then in your loop you can do:

System.out.println(faces[roll-1].replace('x', ch));
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
  • IMO, the array solution is cleaner than the `switch` in the accepted answer. The array construct was probably already taught; OP mentions other flow control structures were not. – banncee Sep 28 '18 at 14:27
  • @banncee We will get arrays next week. So using either switch case/if else is easier for me – N3ur0 Sep 28 '18 at 14:51
0

To convert Math.random() into a die roll, multiply it by the number of possibilities, add the lowest possibility, and convert to int. There are six possibilities with a die. The lowest possibility is a 1. So we get:

(int) (Math.random()*6 + 1)

So, your loop would look like this:

int roll;
do {
    roll = (int) (Math.random()*6 + 1);

    // Display die
} while (roll != 6);

Now how do we display the die with the character the user gave? You mentioned using printf. With printf, %c is the format string for a character. 1$ means to use the first argument. %1$c over and over again would then print the first argument (which should be a char) over and over again.

Here's how to use xs with printf to display a roll of 6:

System.out.printf("%1$c %1$c\n%1$c %1$c\n%1$c %1$c\n", 'x');

So your loop would look like this:

int roll;
do {
    roll = (int) (Math.random()*6 + 1);

    switch (roll) {
        case 1: // printf a 1
                break;
        case 2: // printf a 2
                break;
        case 3: // printf a 3
                break;
        case 4: // printf a 4
                break;
        case 5: // printf a 5
                break;
        case 6: System.out.printf("%1$c %1$c\n%1$c %1$c\n%1$c %1$c\n", character2);
                break;
    }
} while (roll != 6);
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
  • I think this is the best explanation. But how do I convert the roll number to the 'x's? So it displays the rolled number in characters instead? – N3ur0 Sep 28 '18 at 13:53
  • I've added how to do that at the bottom of my answer. You'll need to do the ASCII art for 1 through 5, though. You can also use `if`-`else if`-`else` instead of a `switch`. – Chai T. Rex Sep 28 '18 at 13:57
  • Thanks alot. We didn't learn much about switch, case and break, more about if and else, but I didn't think if and else were needed in this exercise. – N3ur0 Sep 28 '18 at 14:00
  • 1
    @Pluto477, you can look at this particular `switch` as if it was a long list of `if (roll == 1) {} else if (roll == 2) {...` and so on. You can definitely write the code that does the same thing as this switch with an if-else-if tree. – M. Prokhorov Sep 28 '18 at 14:02
  • @Pluto477 You're welcome. Unless you use a `String[]` array or something like that to hold the format strings, some form of branching (`if` or `switch`) will be needed to pick the right ASCII art. With `if` and `else`, it would be loosely `if (roll == 1) printf 1; else if (roll == 2) printf 2; …; else printf 6`. – Chai T. Rex Sep 28 '18 at 14:02
  • @ChaiT.Rex One question though, what does the '$' do exactly in the printf command? I thought %1s was enough in my own code. – N3ur0 Sep 28 '18 at 14:09
  • The `$` means to use the argument just mentioned. So, `%2$c` would use the second argument, since `2` was mentioned just before `$`. That refers to the second data argument here: `printf("…", firstArgument, secondArgument, thirdArgument)`. So, `%1$c` uses the first argument over and over again. Here's [a guide on how to decode (and make up your own) format string specifier](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax). This is the `[argument_index$]` part. – Chai T. Rex Sep 28 '18 at 14:14
0

You can use this as an output with if else, just take the random number that created by Math.Random();.

if (YOURRANDOMNUMBER== 1) {
    System.out.println("* * * * *");
    System.out.println("*       *");
    System.out.println("*   #   *");
    System.out.println("*       *");
    System.out.println("* * * * *");
}

if (YOURRANDOMNUMBER== 2) {
    System.out.println("* * * * *");
    System.out.println("*    #  *");
    System.out.println("*       *");
    System.out.println("*  #    *");
    System.out.println("* * * * *");
}
if (YOURRANDOMNUMBER== 3) {

    System.out.println("* * * * *");
    System.out.println("*    #  *");
    System.out.println("*   #   *");
    System.out.println("*  #    *");
    System.out.println("* * * * *");
}

if (YOURRANDOMNUMBER== 4) {
    System.out.println("* * * * *");
    System.out.println("*  # #  *");
    System.out.println("*       *");
    System.out.println("*  # #  *");
    System.out.println("* * * * *");
}

if (YOURRANDOMNUMBER== 5) {

    System.out.println("* * * * *");
    System.out.println("*  # #  *");
    System.out.println("*   #   *");
    System.out.println("*  # #  *");
    System.out.println("* * * * *");
}
if (YOURRANDOMNUMBER== 6) {

    System.out.println("* * * * *");
    System.out.println("*  # #  *");
    System.out.println("*  # #  *");
    System.out.println("*  # #  *");
    System.out.println("* * * * *");
    System.exit(0);
    }

Its termitate the program when you go in YOURRANDOMNUMBER== 6

Arty
  • 859
  • 2
  • 12
  • 31