java - how to use value of one switch case in another switch case: value is Scanner inputs? -
i have 2 nested switches , wanted use value of 1 switch case in switch case. in below example, wanted use double variable temp_usr , pass argument method (cels()) in switch case, how can this?
switch( switch1){ case 1: { system.out.println(" have selected celsius"); scanner temp_ip= new scanner(system.in); system.out.println("please enter temperature in celsius"); double temp_usr= temp_ip.nextdouble(); } break; case 2: ............... case 3: ............... switch(switch2) { case 1: { system.out.println("convert celsius"); system.out.println(cels(arg)); /*this argument should take value of temp_usr*/ } break; case 2: ......... case 3: .........
the variable visible inside block defined. if want open visibility declare outside switch. try this:
double temp_usr= 0.0; //declaring here switch make visible in following code switch( switch1){ case 1: { system.out.println(" have selected celsius"); scanner temp_ip= new scanner(system.in); system.out.println("please enter temperature in celsius"); temp_usr= temp_ip.nextdouble(); } break; case 2: ............... case 3: ............... switch(switch2) { case 1: { system.out.println("convert celsius"); system.out.println(cels(arg)); /*this argument should take value of temp_usr*/ } break; case 2: ......... case 3: .........
Comments
Post a Comment