The program should run calculations for different shapes as different cases nested inside a while loop. Heres the code:
package Lab_7;
import java.util.*;
public class compar {
public static void main(String [] args){
Scanner d = new Scanner(System.in);
boolean start = true;
while(start){
System.out.print("Would you like to start the program?: ");
String answer1 = d.nextLine();
switch (answer1){
case "yes":
System.out.println("Which shape would you like to use to compute area/perimeter?: ");
String answer2 = d.nextLine();
if(answer2.equals("circle")){
try{
System.out.print("Enter radius: ");
int answer3 = d.nextInt();
Circle c = new Circle(answer3);
double area = c.computeArea();
double perimeter = c.computePerimeter();
System.out.println("Area = " + area + " & perimter = " + perimeter );
break;
}
catch(Exception e){
System.out.println("Error!");
break;
}
}
case "no":
System.out.println("Program Terminating...");
start = false;
break;
default:
System.out.println("bug");
continue;
}
}
d.close();
}
}
However, after running the first successful run, the program should loop back to the beginning (asking the user to start the program?) but instead this happens:
Would you like to start the program?: yes
Which shape would you like to use to compute area/perimeter?:
circle
Enter radius: 10
Area = 314.16 & perimter = 62.832
Would you like to start the program?: bug
Would you like to start the program?:
I could use a bunch of if-statements but I really need to know why after the first successful run, my program:
- Skips all cases and executes the default statement, then loops back to the first print statement and finally waits for input?