Creating a method and passing numbers
Hey everyone I am very new to java, learning only the basics in a computer class(high school).
I need to know how to create a method, and pass integers to it, in the form of a range (example:1-6). This method will be generating a random integer from this range. Any tips or examples would be appreciated, whole program code is not necessary.
Thanks,
Nick
[379 byte] By [
x4ndera] at [2007-10-3 5:22:06]

If you want to pass a range, do it with two integers, like this:
myObject.myMethod(1, 6);
There's no "range" datatype in Java, although you could easily create one if you really needed it.
For example:
public class Range {
public final int start;
public final int end;
// rest left as exercise
}
I don't think you need that, however.
Use java.util.Random to get random numbers, including nextInt which gives you random ints in a range from 0 to some end point.Very useful for getting random positions from an array. If you want to change the range so it doesn't start just at zero, then add an offset.
Thanks for the tip, although i understand this way of generating an integer.The thing my teacher is stressing is the part about creating the method, and then passing the range to it from outside of the method(if this is possible).
public int createRandomNumber(int low, int high) {
// generate a number between high and low
// using the Random class and return it
}