First some code:
import java.util.*;
//...
class TicTacToe
{
//...
public static void main (String[]arg)
{
Random Random = new Random() ;
toerunner () ; // this leads to a path of
// methods that eventualy gets us to the rest of the code
}
//...
public void CompTurn (int type, boolean debug)
{
//...
boolean done = true ;
int a = 0 ;
while (!done)
{
a = Random.nextInt(10) ;
if (debug) { int i = 0 ; while (i<20) { System.out.print (a+", ") ; i++; }}
if (possibles[a]==1) done = true ;
}
this.board[a] = 2 ;
}
//...
} //to close the class
Here is the error message:
TicTacToe.java:85: non-static method nextInt(int) cannot be referenced from a static context
a = Random.nextInt(10) ;
^
What exactly went wrong? What does that error message "non static method cannot be referenced from a static context" mean?
解决方案
You are calling nextInt statically by using Random.nextInt.
Instead, create a variable, Random r = new Random(); and then call r.nextInt(10).
It would be definitely worth while to check out:
Update:
You really should replace this line,
Random Random = new Random();
with something like this,
Random r = new Random();
If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.