Dice Roll
Collapse Content
Show 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.
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
/users/1550
Jan 3, 2:30 PMHaha, 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;
public class Execution {
static int a = 0;
static int b = 0;
static int c = 0;
public static void main(String[] args) {
}
Amboss
Mar 5, 3:33 PMthere some kind of mistake? Method contain two params, why does it can calculating with only one param?????
David
Mar 22, 12:07 PMif a and b are the same, then its essentially one parameter. (a+b) * 2 is the same as a * 4
Greg
May 22, 4:24 PMDude can someone please post the answer..
Krondo
Jun 9, 3:14 PMReally 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.
Cherry Chen
Jul 13, 11:09 PMI 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.
Learneroo
Jul 13, 11:15 PM@Cherry, you left out the closing curly bracket of the method when you copied the code.
Adrian Coroi
May 24, 7:46 AMThe 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.
chima oscar
Jul 26, 11:50 AMVery interesting. I went further to add an extra if statement that allows only numbers between 1 and 6.
TJ Duarte
Jul 30, 8:40 AMhere's mine :
import java.util.Scanner;
public class Main {
Ershad
Sep 25, 11:10 AMI do it. It was interesting.
Mony Jang
Feb 24, 1:31 AMPlease 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 {
}
sumo
Feb 20, 11:41 PMthis 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);
}
Janusz
Mar 15, 2:02 PMRandom rand = new Random();
ddds
Jan 22, 11:34 AMcheck the logic this is correct one :
if(a==b){
return (a+b)+(a+b);
}
else{
return a+b;
}
Dixit
Jul 25, 5:07 AMshowing error
Dheeraj Parmar
Aug 4, 12:47 PM#include
using namespace std;
int func(int a , int b){
int sum=a+b;
if(a != b){
return sum;
}
int main(){
int a=0;
int b=0;
cin>>a>>b;
cout<<func(a,b);
}
Roger
Aug 13, 5:59 PMlet 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
}
}