I am having trouble passing a random object as an argument.
I made a new file to test it and I got the same errors. Can anyone tell me what is wrong with this?
import java.util.*;
public class Practice {
public static void main(String[] args) {
Random r = new Random(1234);
Spring (Random r);
}
void Spring (Random r) {
r.nextInt(20);
}
}
The errors I get are:
/tmp/Practice.java:5: error: ')' expected
Spring (Random r);
^
/tmp/Practice.java:5: error: illegal start of expression
Spring (Random r);
^
2 errors
You have a couple issues....
This is what you should be doing (see comments for explanation):
public static void main(String[] args) {
Random r = new Random(1234);
//don't need Random here
//also call static method
Practice.Spring (r);
}
//since you are calling a static method, you need to declare it static
//also it's good practice to add the methods access modifier.
private static void Spring (Random r) {
r.nextInt(20);
}
To call Spring (...) you will first have to create an instance of your class. To achieve this you must call the constructor:
Practice myPractice = new Practice();
After this you can call your Spring method:
myPractice.Spring(r); //Random must be left out at this place - only when declaring the function interface
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.