vendredi 26 février 2021

DiceRoll Switch statement / if statement working, but output only returning zeroes

So my last question got a reply that really helped, and I was able to work out why my class was not working. Here is my revised code for the Dice Class:

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 die1;
  }

  public int getDie2() {
    return die2;
  }

  public int getTotal() {
    return total;
  }

  public boolean checkDouble() {
    if (die1 == die2) {
     doubles = true;
   } else {
      doubles = false;
    } 
    return doubles; 
  }

  public String getName() {
    if (checkDouble() == true) {
      if (die1 == 1 && die2 == 1) {
        name = "Snake Eyes";
      }
      if (die1 == 2 && die2 == 2) {
        name = "Ballerina";
      }
      if (die1 == 3 && die2 == 3) {
        name = "Brooklyn Forest";
      }
      if (die1 == 4 && die2 == 4) {
        name = "Square Pair";
      }
      if (die1 == 5 && die2 == 5) {
        name = "Puppy Paws";
      }
      if (die1 == 6 && die2 == 6) {
        name = "Boxcars";
      } 
    } else {
       switch (getTotal()) {
        case 3:
          name = "Acey-Deucy";
          break;
        case 4:
          name = "Easy Four";
          break;
        case 5:
          name = "Fever Five";
          break;
        case 6:
          name = "Easy Six";
          break;
        case 7:
          name = "Benny Blue, You're All Through";
          break;
        case 8:
          name = "Easy Eight";
          break;
        case 9:
          name = "Nina";
          break;
        case 10:
          name = "Easy Ten";
          break;
        case 11:
          name = "Yo-Eleven";
          break;
        default:
          name = "Error - Something went wrong";
      }
     } 
   return name;
   }
}

And my code for the actual class that prints out the dice rolls:

import java.util.Scanner;

public class RollSC {
  public static void main(String []args) {

    Dice roll = new Dice();
    Scanner input = new Scanner(System.in);

    System.out.println("Roll 'Em!" + "\n~~~~~~~~~");
    System.out.println("\nYou get to roll the dice two times.\nThe value of both rolls will be added 
    to calculate a total.");

    System.out.println("\nPress enter to roll the die... ");
    input.nextLine();

    System.out.println("Roll #1: " + roll.getDie1());

    System.out.println("\nPress enter to roll the die... ");
    input.nextLine();

    System.out.println("Roll #2: " + roll.getDie2());

    System.out.println("\n\n" + roll.getDie1() + " + " + roll.getDie2() + " = " + roll.getTotal());

    System.out.println(roll.getName() + "!");

  }
}

Now, the only problem that I am getting is that my output only returns zero for the random number. Here is the output I get every single time:

Roll 'Em!
~~~~~~~~~

You get to roll the dice two times.
The value of both rolls will be added to calculate a total.

Press enter to roll the die... 

Roll #1: 0

Press enter to roll the die... 

Roll #2: 0


0 + 0 = 0
null!

I just need some help as to how to fix this output. Thanks!

Aucun commentaire:

Enregistrer un commentaire