Quote:
Originally Posted by weemanbran
This is my assignment:
Write a method called validate that accepts three integer parameters. The first two parameters represent a range, and the purpose of the method is to verify that the value of the third parameter is in that range. You may assume that the first parameter is less than or equal to the second. If the third parameter is not in the specified range, the method should prompt the user and read a new value. This new value should be tested for validity as well. The method should only return to the calling method once a valid value has been obtained, and it should return the valid value.
This is what I have so far:
-
{
-
public int validate (int x, int y, int z)
-
{
-
while((z < x) || (z > y)) {
-
-
// body here
-
}
-
return z;
-
}
-
}
-
I am really bad in Java and have no idea if I am heading in the right direction on this, but how would I make it so I can return the z? Any help would be appreciated
Your control flow logic is fine (assuming x <= y); all you have to do now in the
body of the while loop is tell the user that z is incorrect and read a new value
for it. Check the
Scanner class and read the appropriate article in the
Howtos section (it's all in there).
kind regards,
Jos