使用while循环

时间:2016-02-13 08:20:52

标签: java calculator

我被指派创建一个计算器,在输入时获得正弦,余弦,正切,平方根和数字的自然对数。但是,我在循环时遇到困难。我不明白为什么我的while函数正在跳过我的switch语句而只是反复循环启动问题。

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.");
    }

}

有人可以展示我在while循环中包含switch语句的方式吗?感谢。

1 个答案:

答案 0 :(得分:1)

switch块中,您需要修改case语句。 您需要为您的操作提供case语句,而不是case1, case2, case3等,如下所示:

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();

            }

希望这有帮助!