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.

  • Please can someone help. I can't figure out what I'm doing wrong in this question.
    Here is my code
    import java.util.Scanner;

    public class DiceRolling {

    cont...
  • this is so simple...use the same logic as you used in earlier challange of if else.....
    ie;
    if (a==b){
    return (a+b)*2;
    }else
    return (a+b);
    }

  • Random rand = new Random();

        int a = rand.nextInt(7);
        int b = rand.nextInt(7);
    
  • check the logic this is correct one :
    if(a==b){
    return (a+b)+(a+b);
    }
    else{
    return a+b;
    }

  • showing error

  • #include
    using namespace std;

    int func(int a , int b){
    int sum=a+b;
    if(a != b){
    return sum;

    }
    else if(a == b){
        return sum*2;
    }
    return 0;
    

    }
    int main(){
    int a=0;
    int b=0;
    cin>>a>>b;
    cout<<func(a,b);

    }

  • let a = 0 + Math.floor(Math.random()* 6) + 1;
    let b = 0 + Math.floor(Math.random()* 6) + 1;

    function diceRoll() {
    let sum = a + b;
    if (a == b) {
    return sum *= 2
    } else {
    return sum
    }
    }

Contact Us
Sign in or email us at [email protected]