c# - Method must return a value -


the exercise:

write method rolls 3 dice. passed number of dice roll , returns number 1 6 each of dice rolled , 0 dice not rolled. have pass 4 parameters , either use out or ref keyword or pass array.

now code have far:

    int moredice()     {         random rndom = new random();         int nummer = rndom.next(1, 6);         boolean[] barr = { true, false, true, false, true, false };         (int = 0; < 3; i++)         {              if (barr[nummer] == true)                 {                 return nummer;                 }              else                 {                 return 0;                 }         }      } 

now visual studio tells me have return something, either way. can't seem return null.

and inserted else last i in loop isn't reachable.

is able me or give me slight push in right direction?

there lots of comments being thrown @ , case homework assignments.

first off assignment asking 3 dice, not more less therefore method need understand how many dice being rolled. write method signature in many ways. example use :

void rolldice(int dicecount) {     if(dicecount <= 0 || dicecount > 3)          throw new argumentexception("dice must between 1 3"); } 

this lets determine amount of dice roll... use ref or out parameters, why. caller should know how many dice want roll , expect return value each dice roll. can return int[] result as.

 int[] rolldice(int dicecount)  {     if(dicecount <= 0 || dicecount > 3)          throw new argumentexception("dice must between 1 3");      int[] result = new int[dicecount];      return result;  } 

now return int[] of amount of dice requested roll within 1 - 3 parameter guideline. missing actual dice roll. suggestion using static random (or better random solution) , fill result array roll results. hope helps..

edit missed part of up-to 3 dice not rolled zero. easy handle. ensure dice count greater or equal 1 , calculate rest zeros as.

int[] rolldice(int dicecount)    {        if(dicecount <= 0)            throw new argumentexception("dice must @ least 1");          int[] result = new int[dicecount];         for(int i=0;i<dicecount;i++)         {             if(i < 3)             {                 //roll die...                 result[i] = 1;//roll die (not 1, use random)             }             else             {                 result[i] = 0;             }       }        return result;     } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -