Nyctrimly
Nyctrimly

Reputation: 23

Making a simple calculator for Java class using while loop

I was assigned with creating a calculator that gets the Sine, Cosine, Tangent, Square Root and natural log of a number when inputted. However, I am having difficulties wrapping my mind around while loops. I don't understand why my while function is skipping my switch statement and just looping the priming question over and over.

package week.pkg3.assignment.pkg3;
import java.util.Scanner;

public class Week3Assignment3 {


    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);

        //Details the program and asks for a function and value.
        System.out.println("This caluclator requires you to enter a function and a number.\n The functions are as follows:\n S - sine \n C - cosine \n T - tangent \n R - Square Root"
                + "\n N - natural log \n X 0 - Exit Program \n \n Please enter a function then a value.");

        //Stores the answer into input and converts the character to upper case.
        String input = in.nextLine().toUpperCase();

        //Assigns the char operation to the first character of the string.
        char operation = input.charAt(0);

        //Assigns the remaining numbers to the string sValue.
        String sValue = input.substring(2);

        //Converts the string sValue into a double.
        double dNum = Double.parseDouble(sValue);

        //While the input is anything but X 0 the loop will continue.
        while(!"X 0".equals(input))
            {
        //Switch which takes the character, 'operation', and according to whatever is input applies the math function.        
        switch(operation)
                {
            //The sine function.
            case 1:
                operation = 'S';
                double sine = Math.sin(dNum);
                System.out.println("The sine of your number is " + sine);
                break;     
            //The cosine function.    
             case 2:
                operation = 'C';
                double cosine = Math.cos(dNum);
                System.out.println("The sine of your number is " + cosine);
                break;      
            //The tangent function.    
             case 3:
                operation = 'T';
                double tangent = Math.tan(dNum);
                System.out.println("The sine of your number is " + tangent);
                break;     
            //The square root function.    
             case 4:
                operation = 'R';
                double Sq = Math.sqrt(dNum);
                System.out.println("The sine of your number is " + Sq);
                break;      
            //The natural log function.    
             case 5:
                operation = 'N';
                double log = Math.log(dNum);
                System.out.println("The sine of your number is " + log);
                break;      
                }
                //I'd like for the program to ask this after the switch, and if it is anything but X 0 for exit it will repeat the process.

                System.out.println("This caluclator requires you to enter a function and a number.\n The functions are as follows:\n S - sine \n C - cosine \n T - tangent \n R - Square Root"
                + "\n N - natural log \n X 0 - Exit Program \n \n Please enter a function then a value.");
                input = in.nextLine().toUpperCase();

            }
        //The exit.
        System.out.println("Thanks for using the calculator.");
    }

}

Could someone show how I include the switch statement in my while loop? Thanks.

Upvotes: 1

Views: 1306

Answers (1)

Ravi Ranjan
Ravi Ranjan

Reputation: 740

Inside your switch block you need to modify the case statements. Instead of case1, case2, case3, etc, you need to provide the case statement for your operation, like below:

switch(operation)
                {
            //The sine function.
            case 'S':
                operation = 'S';
                double sine = Math.sin(dNum);
                System.out.println("The sine of your number is " + sine);
                break;     
            //The cosine function.    
             case 'C':
                operation = 'C';
                double cosine = Math.cos(dNum);
                System.out.println("The sine of your number is " + cosine);
                break;      
            //The tangent function.    
             case 'T':
                operation = 'T';
                double tangent = Math.tan(dNum);
                System.out.println("The sine of your number is " + tangent);
                break;     
            //The square root function.    
             case 'R':
                operation = 'R';
                double Sq = Math.sqrt(dNum);
                System.out.println("The sine of your number is " + Sq);
                break;      
            //The natural log function.    
             case 'N':
                operation = 'N';
                double log = Math.log(dNum);
                System.out.println("The sine of your number is " + log);
                break;      
                }
                //I'd like for the program to ask this after the switch, and if it is anything but X 0 for exit it will repeat the process.

                System.out.println("This caluclator requires you to enter a function and a number.\n The functions are as follows:\n S - sine \n C - cosine \n T - tangent \n R - Square Root"
                + "\n N - natural log \n X 0 - Exit Program \n \n Please enter a function then a value.");
                input = in.nextLine().toUpperCase();

            }

Hope this helps!

Upvotes: 1

Related Questions