简体   繁体   中英

Whats wrong with my Random?

Here's my import statement:

import java.util.*;

Here it is in main:

    Random Rand = new Random() ; 

Here it is in a public void method :

            int a = 0 ; 
            while (!done) 
        { 
            int a = Rand.nextInt(10) ;
            if (debug) stuff ; 
            if (possibles[a]==1) done = true ; 
        } 

Here's the error message i get:

    TicTacToe.java:85: cannot find symbol
    symbol  : method nextInt(int)
    location: class Rand
                a = Rand.nextInt(10) ;
                        ^

Whats going wrong here? it seems like i've done everything right to me.

It appears that you have defined a class called Rand somewhere in your project. Don't use this for a variable name. I'd suggest using random with a small r.

Also, this line is illegal:

int a - 0 ; 

You should probably remove it as otherwise you are defining a twice.

 location: class Rand 

You apparently have a class named Rand in the same package or in the imports. Rename the variable name from Rand to rand according to the Sun Java Naming Conventions and it will work fix the particular problem .

You defined Rand in the main method and tried to use it on your public void method . It is out of scope.

Try defining Rand in the same method ( and use lowercase this time)

Something like:

in main:

Random Rand = new Random();

In your method:

        Random rand = new Random();
        int a = 0 ; 
        while (!done) { 
            int a = rand.nextInt(10) ; //<-- the one declared above
            if (debug){ stuff ; }
            if (possibles[a]==1){ done = true ; } 
        }

BTW use braces always even in 1 line if's

It sounds like Rand is a local variable in the main method.

It's not in scope when the "public void method" is invoked. So, the compiler is interpreting the identifier Rand as a class name. Coincidently, your class must be named Rand , so the compiler looks for a static method called nextInt() , and fails.

To fix it, you have a couple of options. Make the Random instance a local variable in the "public void method" that you created (by passing it as a parameter from the main method or other caller, or by instantiating within the method). Alternatively, you could declare a private static variable Rand . That variable would be in scope when invoking the method.

By the way, the convention for Java is that variables start with a lower-case letter. Types (classes, interface, and enums) start with a capital. Breaking this convention makes your code look really strange to a Java programmer.

The compiler told you the error was in location: class Rand .

Why does it think Rand is a class? Do you have a definition of class Rand in your code?

What happens if you call your Random object r instead of Rand ?

尝试将Variable Rand重命名为其他名称..

The error message indicates that Java is looking for a method called nextInt taking an int on a class called Rand .

For some reason Java thinks that it has to look at that class instead of your variable. Is there a class called Rand in your program?

This kind of confusion can easily be avoided by following the naming conventions and starting variable names with a lower-case letter:

 Random rand = new Random(); 
 int a = rand.nextInt(10);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM