Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to convert radians to degrees but I keep getting errors when implementing it into my code. I'm sure there is something simple I am missing but I just can't get what it is.

What I have tried:

Java
package methods;
/**
 *
 * @author stephenwessels
 */
import java.util.Scanner;
public class Methods 
{
    Scanner in = new Scanner(System.in);
    public int rad2deg()
    {
        int deg;
        int rads;
        deg = in.nextInt();
        rads = in.nextInt();
        return deg;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
       Scanner in2 = new Scanner(System.in);
        
        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.");

        String input = in2.nextLine().toUpperCase();

        char operation = input.charAt(0);

        String sValue = input.substring(2);

        double dNum = Double.parseDouble(sValue);
        

        while(!"X 0".equals(input))
            {
        switch(operation)
                {
            case 'S':
                double sine = Math.sin(dNum);
                System.out.println("The sine of your number is " + sine);
                System.out.println("The sine of your number in degrees is " + deg);
                break;     
             case 'C':
                double cosine = Math.cos(dNum);
                System.out.println("The cosine of your number is " + cosine);
                System.out.println("The cosine of your number in degrees is " + deg);
                break;      
             case 'T':
                double tangent = Math.tan(dNum);
                System.out.println("The tangent of your number is " + tangent);
                System.out.println("The tangent of your number in degrees is " + deg);
                break;     
             case 'R':
                double Sq = Math.sqrt(dNum);
                System.out.println("The square root of your number is " + Sq);
                break;      
             case 'N':
                double log = Math.log(dNum);
                System.out.println("The double log of your number is " + log);
                break;      
                }
                input = in2.nextLine().toUpperCase();
            }
        System.out.println("Thanks for using the calculator.");
    }

}
Posted
Updated 7-Feb-18 22:27pm
v2
Comments
wseng 11-Feb-18 9:47am    
Did the answer helped ? If yes, please accept the solution.

1 solution

You should set static int deg as global value, not just declare it inside rad2deg function.
public class JavaApplication26 {
    static int deg;  // declare it here
    Scanner in = new Scanner(System.in);
     .....
Between, where did function rad2deg get called ?
 
Share this answer
 
v2
Comments
Barais_19 7-Feb-18 21:48pm    
Thats considered a global variable right?
wseng 7-Feb-18 21:51pm    
Yes.You have to define it as global variable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900