java - How to assign/access variables in if statements on the same level -
so, need access variables assigned in if statements in last if statement. beginner in java , appreciated :)
here compilation error.
exception in thread "main" java.lang.error: unresolved compilation problems: local variable may not have been initialized local variable b may not have been initialized local variable c may not have been initialized local variable d may not have been initialized local variable e may not have been initialized
and here code. need assign variable based on how many iterations have passed, , access variables after have been assigned on last iteration.
public static void main(string[] args){ string a; string b; string c; string d; string e; scanner s = new scanner(system.in); for( int loop = 1; loop <= 6; loop++ ){ if(loop == 1){ system.out.println("please input grade."); = s.nextline(); system.out.println("you said,"+a); continue; } if(loop == 2){ system.out.println("please input grade.");; b = s.nextline(); system.out.println("you said,"+b); continue; } if(loop == 3){ system.out.println("please input grade."); c = s.nextline(); system.out.println("you said," +c); continue; } if(loop == 4){ system.out.println("please input grade."); d = s.nextline(); system.out.println("you said," +d); continue; } if(loop == 5){ system.out.println("please input grade."); e = s.nextline(); system.out.println("you said," +e); continue; } if(loop == 6){ system.out.println("grade 1."+a); system.out.println("grade 1."+b); system.out.println("grade 1."+c); system.out.println("grade 1."+d); system.out.println("grade 1."+e); } } }
you declaring , trying access string. must initialize them first if you're going use variables. declare them doing this:
string a;
you need initialize them this:
string = "";
initialize them before try access them or java's compiler not happy!
Comments
Post a Comment