做一个计算器,但我收到编译错误(已解决)

时间:2019-05-27 18:12:01

标签: java

我正在自学Java,并开始制作计算器,但是在尝试运行代码时遇到两个错误。不知道该怎么办。 我当时正在考虑用字符来实现WHILE语句以创建循环,但是它似乎没有用。真的很精疲力尽,我认为这是个好地方。

编辑:在下面的优秀人员的帮助下,我得以解决了该问题。 我为下面的while创建了一个switch语句,并创建了另一个scanner。谢谢大家的帮助!!

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;
                       ^

2 个答案:

答案 0 :(得分:0)

您可以将double原语类型更改为Double对象,以便能够返回空值。 您也可以将operatorInput转换为字符串并将其读取为input.next() 这是我建议的更改示例:

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;
}

}

答案 1 :(得分:0)

我通过在while(true)上方使用switch来解决了这个问题。另外,我无法使用nextChar(),因为扫描仪不支持它。我能够通过创建一个名为myInput的扫描仪来解决此问题。这使我能够继续通过我的角色选择。完成此操作后,我就可以使用该扫描器myInput.close();来打破循环,请参考上面的更新代码。再次感谢大家!

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