简体   繁体   中英

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable xxx may not have been initialized

I can't understand it.

my error

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The local variable time may not have been initialized
    The local variable time may not have been initialized

    at earth.main(earth.java:15)

My coding

public class Earth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int time ;
        int distance;
        int speed ;

        distance = 150000000;
        speed = 300000;
        distance = speed * time;
        System.out.println(+time);
    }           
}

you have declared int time; but you have not assigned a value to it, so the runtime will have no idea what to do once you try to do speed * time . The compiler detects this and gives you that error.

Try assigning a value to time , like you're doing with speed .

Try this for example:

int time = 10;

You need to initialize it to some
value before you try using it.

The other two variables you initialized already
but the time variable you didn't initialize.

Rule 1:

Run only 100% compiled code. Resolve all compiler errors before running your code.

Rule 2:

Local variables should be initialize before they are getting used. initialize your time variable with some value.

int time = ? ;

Interesting, I can see you just started to learn programming, and you are confused about the relation between programming and math.

You expect programming something like math.

In math, you define an equation:

1500 = 3 * time

And you then derive answer time = 500 .

Programming does NOT work that way. Programming does what machine does, which literally only:

fetch data from some memory
perform simple operation (for instance math operation + - * / )
store result back to some memory

In programming, an equal sign = is way different from its mathematical meaning.

In programming, = is called assignment , means where to store the calculated result. So when you write distance = speed * time; , it's NOT an math equation. To machine, it means:

fetch values from memory "speed" and memory "time"
perform math multiply on the two values
store the result back to memory "distance"

Obviously it's not what you want to do. And because your "time" didn't contain any value, the Java compiler complain on the first step "fetch value from 'time'".

Computer is stupid, it doesn't solve math problem for you, you have to write down your solution clearly.

What you really want is:

time = distance / speed;

fetch values from distance and speed
perform math divide on the two values
store result back to time

Good luck on your study :)

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