positive or negative, and be in either order (smaller first, larger first, or equal)).
Here is my method, but one problem I noticed is that if I input 2 and 9, sometimes I get the output 10, which is clearly not between 2 and 9 inclusive. Also, what if int first and int second are equal? The method is not void, but I do not want to return a value, because there is no integer between them if they are equal?
public static int randomNumber(int first, int second)
{
int randomNum;
if (first<second)
{
randomNum= (int)(second*Math.random()+first);
return randomNum;
}
else if (second<first)
{
randomNum= (int)(first*Math.random()+second);
return randomNum;
}
else // first and second are equal
{
System.out.println("The two numbers are equal");
return first;
}
}
This post has been edited by lolitacharm: 11 July 2011 - 09:19 AM

New Topic/Question
Reply



MultiQuote




|