Dice Roll


Collapse Content

These challenges will involve using if-then-else loops. Here's a simple challenge to get started.

You're programming a backgammon game, and are working on the dice roll method. Given two ints, return their sum. However, if the two numbers are the same, return double their sum.

2-dice

Image from Wikipedia

Challenge

Return the sum (or double sum) of two ints, as above.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • Haha, LOL! I can't solve it now, it's funny! But I solve the problem some other way, I created my own version of code with another logic:

    import java.util.Scanner;

    cont...
  • there some kind of mistake? Method contain two params, why does it can calculating with only one param?????

  • if a and b are the same, then its essentially one parameter. (a+b) * 2 is the same as a * 4

  • Dude can someone please post the answer..

  • Really simple stuff. For those of you stuck on this, remember order of operations and == other than that this should be a piece of cake with some ice cream.

  • I copied the answer in and it was still incorrect... what dafuq this site has bugs and it's teaching me how to code? what is this.

    if(a==b){
        return a * 4;
    }
    else{
        return a+b;
    }
    
  • @Cherry, you left out the closing curly bracket of the method when you copied the code.

  • The problem is with the last set of parameters. It is 2 and 2 and expects an 8, when in fact it should expect a 4.

  • Very interesting. I went further to add an extra if statement that allows only numbers between 1 and 6.

  • here's mine :

    import java.util.Scanner;

    public class Main {

    static int doStuff(int a, int b){
        //your code here
        if(a!=b)
            {
                return a+b;
            }
        else
            {
                return (a+b)+(a+b);
            }
    }
    
  • I do it. It was interesting.

Contact Us