简体   繁体   中英

Making a calculator but I am receiving compiling errors

I am teaching myself Java and started to make a calculator but I'm running into two errors when I try and run the code.

I was thinking about implementing a WHILE loop with the characters to create a loop but it did not seem to work. Really burnt out and I thought this was a good place to ask.

import java.lang.Math;
import java.util.Scanner;

public class Calculator 
{

    public static void main(String[] args) 
    {
        double numberOne = 0;
        double numberTwo = 0;
        String operatorInput;
        Scanner myInput = new Scanner(System.in);

       System.out.print("Enter first number: ");
       numberOne = myInput.nextDouble();

       System.out.print("Enter second number: ");
       numberTwo = myInput.nextDouble();

       System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
       operatorInput = myInput.nextLine();

       double result = calculate(numberOne, numberTwo, operatorInput, myInput);

       System.out.printf("The result is: %.2f%n", result);

       myInput.close();
    }

    public static double calculate(double numberOne, double numberTwo, String operatorInput, Scanner myInput)
    {
        while(true)
        {
            switch (operatorInput)
            {
                case("/"):
                {
                    myInput.close();
                    return divideNumbers(numberOne, numberTwo);
                }
                case("*"):
                {
                    myInput.close();
                    return multiplyNumbers(numberOne, numberTwo);
                }
                case("+"):
                {
                    myInput.close();
                    return addNumbers(numberOne, numberTwo);
                }
                case("-"):
                {
                    myInput.close();
                    return subtractNumbers(numberOne, numberTwo);
                }
                default: 
                {
                    System.out.println("Invalid Input");
                    System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
                    operatorInput = myInput.nextLine();
                }
            }
        }
    }

    public static double divideNumbers(double numberOne, double numberTwo)
    {
        return numberOne / numberTwo;
    }
    public static double multiplyNumbers(double numberOne, double numberTwo)
    {
        return numberOne * numberTwo;
    }
    public static double addNumbers(double numberOne, double numberTwo)
    {
        return numberOne + numberTwo;
    }
    public static double subtractNumbers(double numberOne, double numberTwo)
    {
        return numberOne - numberTwo;
    }
}
Calculator.java:27: error: cannot find symbol
       operatorInput = input.nextChar();
                            ^
  symbol:   method nextChar()
  location: variable input of type Scanner
Calculator.java:55: error: incompatible types: <null> cannot be converted to double
                return null;
                       ^

you can change double primitive type to Double object to be able to return null value. also you can convert operatorInput to string and read it as input.next() here's example for what changes I suggest:

import java.util.Scanner;

public class Calculator 
{
public static void main(String[] args) 
{
    double numberOne = 0;
    double numberTwo = 0;
    String operatorInput;

Scanner input = new Scanner(System.in);

   System.out.print("Enter first number: ");
   numberOne = input.nextDouble();

   System.out.print("Enter second number: ");
   numberTwo = input.nextDouble();

   System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
   operatorInput = input.next();
   System.out.println(calculate(numberOne, numberTwo,operatorInput));

}



public static Double calculate(double numberOne, double numberTwo, String operatorInput)
{
    switch (operatorInput)
    {
        case("/"):
        {
            return divideNumbers(numberOne, numberTwo);
        }
        case("*"):
        {
            return multiplyNumbers(numberOne, numberTwo);
        }
        case("+"):
        {
            return addNumbers(numberOne, numberTwo);
        }
        case("-"):
        {
            return subtractNumbers(numberOne, numberTwo);
        }
        default: 
        {
            System.out.println("Invalid Input");
            return null;
        }
    }

}

public static double divideNumbers(double numberOne, double numberTwo)
{
    return numberOne / numberTwo;
}
public static double multiplyNumbers(double numberOne, double numberTwo)
{
    return numberOne * numberTwo;
}
public static double addNumbers(double numberOne, double numberTwo)
{
    return numberOne + numberTwo;
}
public static double subtractNumbers(double numberOne, double numberTwo)
{
    return numberOne - numberTwo;
}

}

I managed to solve the issue by using a while(true) on top of the switch . Also, I could not use the nextChar() because it wasn't supported by the scanner. I was able to fix this by creating a scanner called myInput . This allowed me to continue on passed me character selection. Once that was done I was able to break the loop with that scanner myInput.close(); refer to updated code above. Thanks again, everyone!

 double numberOne = 0;
        double numberTwo = 0;
        String operatorInput;
        Scanner myInput = new Scanner(System.in);

       System.out.print("Enter first number: ");
       numberOne = myInput.nextDouble();

       System.out.print("Enter second number: ");
       numberTwo = myInput.nextDouble();

       System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
       operatorInput = myInput.nextLine();

       double result = calculate(numberOne, numberTwo, operatorInput, myInput);

       System.out.printf("The result is: %.2f%n", result);

       myInput.close();
    }

    public static double calculate(double numberOne, double numberTwo, String operatorInput, Scanner myInput)
    {
        while(true)
        {
            switch (operatorInput)
            {
                case("/"):
                {
                    myInput.close();
                    return divideNumbers(numberOne, numberTwo);
                }
                case("*"):
                {
                    myInput.close();
                    return multiplyNumbers(numberOne, numberTwo);
                }
                case("+"):
                {
                    myInput.close();
                    return addNumbers(numberOne, numberTwo);
                }
                case("-"):
                {
                    myInput.close();
                    return subtractNumbers(numberOne, numberTwo);
                }
                default: 
                {
                    System.out.println("Invalid Input");
                    System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
                    operatorInput = myInput.nextLine();
                }
            }
        }
    }

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