Question

In: Computer Science

Why won't the program run the code after the first switch statement. It shows Press c...

Why won't the program run the code after the first switch statement. It shows Press c to continue or q to quit, but does not give the user the option to fill it in. Please help to run the code fully. This program is to find the area of shapes the user decides and must give them an option to quit or restart the program. Please provide the explanation and code in JAVA here. Thank you.

import java.util.Scanner;
import java.lang.Math;
public class area
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String square;
String circle;
String rectangle;
String triangle;
  
System.out.println("Please choose one of the shapes you want the area of.");
System.out.println("Square, Circle, Rectangle, Triangle.");
String shape = scan.nextLine();
switch (shape)
{
case "Square":
System.out.println("Please enter the length of one side of the square.");
double squareLength = scan.nextDouble();
double squareArea = (squareLength * squareLength);
System.out.println("The area of your square is "+ squareArea + ".");
break;
case "Circle":
System.out.println("Please enter the radius of the circle.");
double circleRadius = scan.nextDouble();
double circleArea = (circleRadius * circleRadius)*Math.PI;
System.out.println("The area of your circle is " + circleArea + ".");
break;
case "Rectangle":
System.out.println("Please enter the width of the rectangle.");
double rectangleWidth = scan.nextDouble();
System.out.println("Please enter the length of the rectangle.");
scan.nextDouble();
double rectangleLength = scan.nextDouble();
double rectangleArea = (rectangleWidth * rectangleLength);
System.out.println("The area of your rectangle is "+ rectangleArea + ".");
break;
case "Triangle":
System.out.println("Please enter the height of the trangle.");
double triangleHeight = scan.nextDouble();
System.out.println("Please enter the base of the trangle.");
double triangleBase = scan.nextDouble();
double triangleArea = (triangleHeight * triangleBase) / 2;
System.out.println("The area of your triangle is " + triangleArea + ".");
break;
default:
System.out.println("Please choose an option.");
scan.nextDouble();
}
System.out.println("Press c to continue and or q to quit.");
String option = scan.nextLine();
switch (option)
{
case "c":
break;
case "q":
System.out.println("Have a good day!");
break;
}
  
}
}

Solutions

Expert Solution

import java.util.Scanner;
import java.lang.Math;
class Area
{
public static void main(String[] args)
{
   Scanner scan = new Scanner(System.in);
   String square;
   String circle;
   String rectangle;
   String triangle;
   char option;
do
{
   System.out.println("Please choose one of the shapes you want the area of.");
   System.out.println("Square, Circle, Rectangle, Triangle.");
   String shape = scan.nextLine();
   switch (shape)
   {
       case "Square":
           System.out.println("Please enter the length of one side of the square.");
           double squareLength = scan.nextDouble();
           double squareArea = (squareLength * squareLength);
           System.out.println("The area of your square is "+ squareArea + ".");
           break;
       case "Circle":
           System.out.println("Please enter the radius of the circle.");
           double circleRadius = scan.nextDouble();
           double circleArea = (circleRadius * circleRadius)*Math.PI;
           System.out.println("The area of your circle is " + circleArea + ".");
           break;
       case "Rectangle":
           System.out.println("Please enter the width of the rectangle.");
           double rectangleWidth = scan.nextDouble();
           System.out.println("Please enter the length of the rectangle.");
           scan.nextDouble();
           double rectangleLength = scan.nextDouble();
           double rectangleArea = (rectangleWidth * rectangleLength);
           System.out.println("The area of your rectangle is "+ rectangleArea + ".");
           break;
       case "Triangle":
           System.out.println("Please enter the height of the trangle.");
           double triangleHeight = scan.nextDouble();
           System.out.println("Please enter the base of the trangle.");
           double triangleBase = scan.nextDouble();
           double triangleArea = (triangleHeight * triangleBase) / 2;
           System.out.println("The area of your triangle is " + triangleArea + ".");
           break;
       default:
       System.out.println("Please choose an option.");
       break;
   }
   System.out.println("Press c to continue and or q to quit.");
option = scan.next().charAt(0);
}while(option=='c');

}
}

// I fixed all the error and correct the code please check

// If any doubt please comment


Related Solutions

With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based...
Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based on your input and the selection from the keyboard, the program does the followings. If the selection is 1, the program should test the input if it’s positive, negative or zero. For example, the output should be “The selection is 1 to test the input value if it’s positive, negative or equal to zero. The input value 7 is positive” If the selection is...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
11. For each of the following problems, click after Statement, press enter and type a statement...
11. For each of the following problems, click after Statement, press enter and type a statement to accept input data from the keyboard into meaningful variable names of your choice. Your statement includes prompts and conversion functions as needed. a. Accepts your gpa (a real number). Statement: b. Accepts the number of people in your family (an integer number). Statement:
4. Translate the following C code to MIPS assembly (in two separate files). Run the program...
4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;} Submission file: Lab4_4a.asm and Lab4_4b.asm
A) Is it important to have a break after every case in the switch statement?) b)...
A) Is it important to have a break after every case in the switch statement?) b) Explain lain your answer by writing an example of a switch statement and showing the effect of including and excluding the break.                                                                                                                                                               10 points A) String Name = KB.NextLine();       Write a java statement to convert Name to upper case. B) char Letter =KB.next().charAt(0);           Write a java statement to convert Letter to lower case.                                                                                                                                                        5 points c) Write a while loop which...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using semaphores. (Full program)Write code showing the elevator algorithm. c++ Language
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT