1

I'm just trying to figure out this homework and could use some help. NOT LOOKING FOR ANSWERS, just a better understanding. I'm very very new to coding, using jGRASP java. The teacher told us to create this game that generates an output similar to this.

Round 1:

• Player A rolls: 1,2, the highest number is 21 • Player B rolls: 2,1, the highest number is 21 • Result: draw

• Round 2: • Player A rolls: 2,5, the highest number is 52 • Player B rolls: 6,8 the highest number is 86 • Result: Player B wins

• Round 3: • Player A rolls: 1,9, the highest number is 91 • Player B rolls: 9,9 the highest number is 99 • Result: Player B wins

• Final Result: Player B wins!!!

Do I have to declare the numbers as Strings to concatenate them? and do I use if else statements? Specifically, I'm confused on how to arrange the two numbers if one is greater than the other so the greater number will be placed in front.

Anything helps, thank you guys.

Have a good day!

import java.util.Scanner;
import java.util.Random;

public class beatThat
{
   public static void main(String[] args)
   {
      Random r1 = new Random();
      Random r2 = new Random();
      Random r3 = new Random();
      Random r4 = new Random();

      int die1 = r1.nextInt(6) + 1;
      int die2 = r2.nextInt(6) + 1;
      int die3 = r3.nextInt(6) + 1;
      int die4 = r4.nextInt(6) + 1;

      System.out.println("ROUND 1");
      System.out.println("Player A rolls: " + die1 + " and " + die2 +".");
      System.out.println("Player B rolls: " + die3 + " and " + die4 +".");

      if(die1 >= die2)
      {
         System.out.println("Player A's highest number is: " + die1 + die2);
      }
      else if(die2 >= die1)
      {
         System.out.println("Player A's highest number is: " + die2 + die1);
      }
      if(die3 >= die4)
      {
         System.out.println("Player B's highest number is: " + die3 + die4);
      }
      else if(die4 >= die3)
      {
         System.out.println("Player B's highest number is: " + die4 + die3);
      }

    }
}
JJJ
  • 32,902
  • 20
  • 89
  • 102

1 Answers1

0

Simplified a bit your code.

import java.util.Random;

public class Main {

public static void main(String[] args) {

    Random r1 = new Random();
    Random r2 = new Random();
    Random r3 = new Random();
    Random r4 = new Random();

    int die1 = r1.nextInt(6) + 1;
    int die2 = r2.nextInt(6) + 1;
    int die3 = r3.nextInt(6) + 1;
    int die4 = r4.nextInt(6) + 1;

    System.out.println("ROUND 1");
    System.out.println("Player A rolls: " + die1 + " and " + die2 +".");
    System.out.println("Player B rolls: " + die3 + " and " + die4 +".");

    Integer aMax = Integer.max(die1*10 + die2, die2*10 + die1);
    System.out.println("Player A's highest number is: " + aMax);

    Integer bMax = Integer.max(die3*10 + die4, die4*10 + die3);
    System.out.println("Player B's highest number is: " + bMax);

    System.out.println("Result: " +
            (!aMax.equals(bMax) ?
                aMax > bMax ? "Player A wins!" : "Player B wins!"
                :
                "Draw!")
    );
}
}

All I have done is just replacing that not nice if/else statements with with simple building and comparing that two numbers. In this case of two rolls I think it would be just fine. If you consider more, maybe you should try with string permutation and getting max value of values array. Try this.

About numbers concatination. You don't need to cast them as string if at least pair of them are NOT standing first in concatination chain.

This example will make it clean:

System.out.println(1 + 2 + "text"); //"3text"
System.out.println(1 + "text" + 2); //"1text3"
System.out.println("text" + 1 + 2); //"text12"

Hope it'll help you. Cheers.

Peteef
  • 377
  • 1
  • 2
  • 10