Hi guys this is my first post so please forgive me if i make any mistakes. Here is my problem:
public class Main {
public static void main(String[] args) {
int onlineplayer = 0;
int maxplayer;
switch(onlineplayer) {
case 0:
maxplayer = 1;
break;
case 1:
maxplayer = 2;
break;
}
System.out.print(onlineplayer + "/" + maxplayer);
}
This line of code returns this error:
Main.java:13: error: variable maxplayer might not have been initialized
System.out.print(onlineplayer + "/" + maxplayer);
^
While
public class Main {
public static void main(String[] args) {
int onlineplayer = 0;
int maxplayer;
switch(onlineplayer) {
case 0:
maxplayer = 1;
System.out.print(onlineplayer + "/" + maxplayer);
break;
case 1:
maxplayer = 2;
System.out.print(onlineplayer + "/" + maxplayer);
break;
}
}
This one works. I stated learning Java a while ago and I think they are both the same thing. Can someone please explain to me what is the difference and why doesn't the first one work?