math - Java confusion initializing variables within different cases -


my source code below attempting create program calculate area diameter , circumference of circle when user enters a,c, or d. want return correct response depending on user input. managed 3 return within first case earlier separating them proved difficult ideas?

import java.util.scanner;  public class test {      public static void main(string[] args) {         scanner sc = new scanner(system.in);         system.out.print("this program determine area, circumference or diameter circle. type area c circumference , d diameter"); // prompt user input of capitol character          while (!sc.hasnext("[a-z]+")) {             system.out.println(sc.next().charat(0) + " not capital letter! non alphanumeric values not permitted."); // error response unnaceptable character a-z specified range of acceptable characters         }          char c = ' ';         c = sc.next().charat(0); // looking through user input @ character @ position 0          switch (c) {          case 'a':             system.out.print("enter radius: "); //i storing entered radius in floating point             float radius = sc.nextfloat();             float area = ((float) math.pi) * ((float)(radius * radius));             system.out.printf("the area of circle is: %.2f \n", area);             break;          case 'c':             system.out.print("enter radius: "); //i storing entered radius in floating point             float circumference = ((float)(math.pi * 2 * radius));             system.out.printf("the circumference of circle is: %.2f \n", circumference);             break;          case 'd':             system.out.print("enter radius: "); //i storing entered radius in floating point             float diameter = ((float)(radius * 2));             system.out.printf("the diameter of circle is: %.2f \n", diameter);             break;         }     } } 

you define , calculate float radius = sc.nextfloat(); inside case 'a': radius used in 2 other cases. in switch 1 case executed (when there no fall though), therefor, when select case 'c' or 'd' radius variable never defined , you'll error.

to solve this, define , calculate radius outside switch

...  float radius = sc.nextfloat(); switch (c) {  case 'a':     system.out.print("enter radius: "); //i storing entered radius in floating point     float area = ((float) math.pi) * ((float)(radius * radius));     system.out.printf("the area of circle is: %.2f \n", area);     break;  case 'c':     system.out.print("enter radius: "); //i storing entered radius in floating point     float circumference = ((float)(math.pi * 2 * radius));     system.out.printf("the circumference of circle is: %.2f \n", circumference);     break;  case 'd':     system.out.print("enter radius: "); //i storing entered radius in floating point     float diameter = ((float)(radius * 2));     system.out.printf("the diameter of circle is: %.2f \n", diameter);     break; } ... 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -