Create a text-based, menu-driven program that allows the user to choose whether
ID: 3535867 • Letter: C
Question
Create a text-based, menu-driven program that allows the user to choose whether to add, subtract, multiply or divide two numbers. The program should then input two double values from the use, perform the appropriate calculation, and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives two double parameters. The corresponding functions should each display messages indicating which calculation was performed, the values of the parameters, and the result of the calculation.
Explanation / Answer
import java.io.*; class asmd{ public static void main() throws java.io.IOException{ char choice; BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter 1st number:"); String fst = stdin.readLine(); int x = Integer.parseInt(fst); System.out.println("Enter number of sequence:"); String snd = stdin.readLine(); int y = Integer.parseInt(snd); do{ System.out.println("[1] Add"); System.out.println("[2] Subtract"); System.out.println("[3] Multiply"); System.out.println("[4] Divide"); System.out.println("Choice:"); choice = (char) System.in.read(); }while(choice!=5); int sum=x+y; switch(choice){ case 1: System.out.println(" +"+x+" "+y+"= "+sum); break; case 2: System.out.println(" -"+x+" "+y+"= "+(x-y)); break; case 3: System.out.println(" *"+x+" "+y+"= "+(x*y)); break; case 4: System.out.println(" /"+x+" "+y+"= "+(x/y)); break; default: System.out.println("Invalid input!"); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.