Question
I need to add an invalid entry message when someone enters a wrong value but i can't get it to work. Is there a different
I need to add an invalid entry message when someone enters a wrong value but i can't get it to work. Is there a different way to do it than the way i did? Also my subtraction selection is not working right Here is what I have so far. import java.util.Scanner;
public class ReviewApplication {
public static void main(String[] args) { Scanner keyboard = new Scanner (System.in);
double sum = 0; // Initialize variable
double difference = 0; // Initialize variable
double product = 0;// Initialize variable
double divide = 0;// Initialize variable
double[] array = new double[5];// create array
System.out.print ("Enter 5 floating point numbers: "); // print message
for (int i = 0; i < array.length; i++)// for loop {
array[i] = keyboard.nextDouble (); // get input from user
sum = sum + array[i]; // add array }
char response; // declare variable
while (1 == 1) // while loop
{
System.out.println ("Enter a to add numbers"); // print message System.out.println ("Enter s to subtract"); // print message System.out.println ("Enter m to multiply"); // print message
System.out.println ("Enter d to divide"); // print message System.out.println ("Enter q to exit"); // print message
System.out.print (">>> "); // print message
response = keyboard.next ().charAt (0);// get user input switch (response) // switch case
{
case 'a': // case a
System.out.println ("Sum of Numbers is " + sum);// print message
break; case 's': // switch case
difference = array[0]; // putting array first element in difference
for (int i = 0; i < array.length; i++)// print message { difference = difference - array[i]; }
System.out.println ("the difference is" + difference);// print message
break;
case 'm': // case m
product = array[0]; // putting array first element in product variable
for (int i = 1; i < array.length; i++) // for loop { product = product * array[i]; // multiplying each element } System.out.println ("the product is " + product);// print message
break;
case 'd':
divide = array[0];// putting array first element in divide variable
for (int i = 1; i < array.length; i++)// for loop {
divide = divide / array[i];// dividing each element with its previous one }
System.out.println ("the answer is" + divide); // print message
break;
case 'q': // case q System.exit (0); System.out.println ("bye!"); // print message if (response != 'a' || response != 's' || response != 'm' || response != 'd'|| response != 'q') System.out.println("Invalid menu slection. Try again."); break; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started