java - How to use variables defined in an if statement outside of it -
i have been trying make calculator , when define variables inside if
statement says:
exception in thread "main" java.lang.error: unresolved compilation problem: blocknum cannot resolved variable @ firsttry.main.main(main.java:57)
now know have define variables outside of if
statements, because of variable scope, when try so:
package firsttry; import java.util.scanner; public class main { public static void main(string args[]){ int blocknum; blocknum = 1;
i error of:
exception in thread "main" java.lang.error: unresolved compilation problem: duplicate local variable blocknum
@ firsttry.main.main(main.java:36)
so how fix problem? have read solution problem variable scope define variable outside of if
statement, doesnt seem work me?
my full code:
package firsttry; import java.util.scanner; public class main { public static void main(string args[]) { int blocknum; blocknum = 1; csv bgather = new csv(); toolname atool = new toolname(); scanner input = new scanner(system.in); //welcoming message system.out.println("welcome minecraft build/excavate calculator" + "\nits suggested use tools suitable block" + "\nthat breaking faster" + ""); //gatheres user variables system.out.println("what block gathering? (use minecraft block ids)"); string block = input.next(); system.out.println("what tool using , 1 using hand , 4 diamond"); int tool = input.nextint(); system.out.println("do want enter dimensions of area excavating" + "or exact block number" + "1 dimensions " + "2 exact number"); int typeofselection = input.nextint(); if (typeofselection == 1) { system.out.println("height of area"); int height = input.nextint(); system.out.println("length of area"); int length = input.nextint(); system.out.println("width of area"); int width = input.nextint(); } else if (typeofselection == 2) { system.out.println("exact amount of blocks"); int blocknum = input.nextint(); } else { system.out.println("errorrrr"); } //csv file stuf string toolname = atool.nameoftool(tool); string blockname = bgather.name(block); double blockbreak = bgather.speed(tool,block); input.close(); //overall calculations if (typeofselection == 2) { system.out.println(blocknum); } system.out.println("you gatherering " + blockname + " using " + toolname + " \nand take " + blockbreak + " seconds per block"); } }
else if (typeofselection == 2){ system.out.println("exact amount of blocks"); int blocknum = input.nextint(); }
if int blocknum
same variable variable declared @ top, remove int
part.
else if (typeofselection == 2){ system.out.println("exact amount of blocks"); blocknum = input.nextint(); }
Comments
Post a Comment