简体   繁体   中英

Calculator int choice = Integer.parseInt(char_a); java

Trying to create a simple calculator on java. No errors show up in the code. But it still doesn't work at all. am I missing anything?

import java.util.Scanner;

public class JavaApplication15 {


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



      System.out.println("This is a calculator. Enter a letter followed by a number to calculate it.");
       System.out.println(" S = sine \n C = Cosine \n T = Tangent \n R = Square Root \n N = natural Log \n X = exit the program");
        String num = in.nextLine();

        String sValue = num.substring(2);
        String char_a = num.substring(0);

       int choice = Integer.parseInt(char_a);
       double dValue = Double.parseDouble(sValue);

       while (choice != 'x'){
        switch(choice){

            case 's':
                Math.sin(dValue);
                System.out.println("The sine of your number is " + dValue);
                break;
            case'c':    
                 Math.cos(dValue);
                 System.out.println("The Cosine of your number is " + dValue);
                  break;
            case't':   
                 Math.tan(dValue);
                 System.out.println("The Tangent of your number is " + dValue);
                  break;
            case'r': 
                 Math.sqrt(dValue);
                 System.out.println("The square root of your number is " + dValue);
                  break;
            case'n':    
                 Math.log(dValue);
                 System.out.println("The Log of your number is " + dValue);
                  break;
            case'x':
                break;

        }
       }

    }

}

I think I see your error.

You're performing operations using the Math class but aren't assigning the result of the operation back to your variable.

For example, Math.cos(dValue); should probably be dValue = Math.cos(dValue);

There are a few problems with your code.

Firstly, you are not using .substring method correctly. It returns everything from the index you specify to the end of your String. So for a user input of "S 4"

sValue equals to "4", but char_a equals to "S 4".

The way you use substring method is:

value = input.substring(2);
operation = input.substring(0,1);

I would actually suggest that you use something like:

params = input.split(" ");

Then you have:

params[0] // as your command

and

params[1] // as your value

This way you don't have to worry about how many symbols each bit actually takes up.

Next, don't convert your command to char like this. My previous suggestion means you should really be using something like

if (params[0].equals("sin")) {

} else if (params[0].equals("cos")) {

} else {
// catch unknown command
}

However, you can convert to char simply by:

sValue.toCharArray()[0]

Also, there is no reason why your switch statement should be in a while loop. There is nothing to be done continuously, it will just keep printing the same answers. And lastly, ajb said, you calculate the values and throw them away, whilst printing the old value. You have to use:

System.out.println("The Tangent of your number is " + Math.tan(dValue));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM