当前位置: 首页 > 知识库问答 >
问题:

我正在用Java构建一个计算器,在执行操作符时遇到了问题

夹谷鸿福
2023-03-14

我对Java相当陌生,以前从未做过编程。在尝试为每个操作符运行代码时,我遇到以下错误。我希望它将用户输入作为字符串接收,但返回一个double作为结果。我知道可能会有几个错误,但我们非常感谢您的帮助。

线程“main”java中出现异常。lang.错误:未解决的编译问题:类型不匹配:无法从字符串转换为双精度运算符-参数类型java未定义。lang.字符串,java。lang.String参数类型java的运算符*未定义。lang.字符串,java。参数类型java的运算符/未定义。lang.字符串,java。lang.String参数类型java的运算符%未定义。lang.字符串,java。lang.String无法对非静态字段数学进行静态引用无法对非静态字段数学进行静态引用命令无法解析为运算符类型

at Calculator.main(Calculator.java:72)
import java.util.Scanner;
import java.lang.Math;

/*@public class for calculator*/
public class Calculator {
    String ADD = "+";
    String SUB = "-";
    String DIV = "/";
    String MUL = "*";
    String MOD = "%";
    String SQRT = "sqrt";
    String POW = "^";
    String Math.PI = "pi";

    public static void main(String[] args) {


        /*@Scanner to read user input */
        Scanner myScanner = new Scanner(System.in);
        boolean continueLoop = true;

        /*@try statement to begin program */
        try {



            String x,y,command;
            double result = 0;
            double myDouble = result;

            /*@print line welcoming user to calculator */
            System.out.print("Welcome to my Calculator! Lets Begin!" + "\n");
            /*@print line asking user to either type HELP or START to begin */
            System.out.print("Type HELP or START to begin: ");
            /*@string to read user input for HELP or START command */
            String firstCommand = myScanner.nextLine();
            if (firstCommand.equals("HELP")) {
                System.out.println("Welcome to my calculator. Make sure to enter only numbers into the first two fields. \nDo NOT divide by 0. \nBe sure to enter the number matching your desired operation.");
                System.out.print("Type HELP or START to begin: ");
                command = myScanner.nextLine();
            }




            Double firstNumber = Double.parseDouble(myScanner.nextLine());
            /*@print statement asking user for first number input */
            System.out.println("Please Enter your First number: ");
            /*@variable x to be read by scanner */

            x = myScanner.nextLine();

            Double secondNumber = Double.parseDouble(myScanner.nextLine());
            /*@print statement asking user for second number input *        
            System.out.println("Please Enter your Second Number: ");

            /*@variable y to be read by scanner */
            y = myScanner.nextLine();

            /*@param converts string into double*/
            Double converted = Double.parseDouble(myScanner.nextLine());    
            /*Print statements with instructions on what commands to put in and where to put in.*/
            System.out.print("\n1: Add. \n2: Sub. \n3: Mult. \n4: Divide. \n5: Modulo. \n6: Pi \n7: Pow.");
            System.out.print("\nEnter your operator: ");

            command = myScanner.nextLine();

            switch(command) {
            /*@returns addition of firstNumber and secondNumber */
            case "1":
                result = (x+y); break;
            /*@returns subtraction of firstNumber and secondNumber */
            case "2":
                result = (x-y); break;
            /*@returns multiplication of firstNumber and secondNumber */
            case "3":
                result = (x*y); break;
            /*@returns division of firstNumber and secondNumber */
            case "4":
                result = (x/y); break;
            /*@returns modulo of firstNumber and secondNumber */
            case "5":
                result = (x%y); break;
            /*@returns Math.PI */
            case "6":
                result = (Math.PI);
            /*@returns power of firstNumber to the second number secondNumber*/
            case "7":
                result = (Math.pow(x, y));
            }
            if(command. >= 1 && command <= 5)
                System.out.println("Your answer is: " + result);

            /*@if statements for all mathematical operators */
            // cannot get sqrt, pow, pie to work


        /*@throws exception if anything other than a number is typed in by user */  
        /*catch exception if anything other than number is keyed in*/
        } catch (InputMismatchException inputMismatchException) {
            /*@error statement printed*/
            System.err.printf("%nException: %s%n", inputMismatchException);
            /*@statement for scanner to read the next line*/
            myScanner.nextLine(); // discard input, so user can try again
            /*@print statement letting user know to try again*/
            System.out.printf("You must enter integers. Please try again %n%n");

        }
            /*@throws exception if zero is keyed in as denominator
            /*exception if zero is keyed in as the denominator in division since you cannot divide by zero*/
        catch (ArithmeticException arithmeticException) {
            /*error statement printed*/
            System.err.printf("%nException: %s%n", arithmeticException);
            /*print statement letting user that 0 is not valid and to please try again
            not able to get the loop to go back and restart*/
            System.out.printf("Zero is an invalid denominator.Please try again. %n %n");
        }
        /*@param finally statement closes scanner*/
        finally {
            myScanner.close();
        } while(continueLoop);

    } 



}

共有1个答案

鲁杜吟
2023-03-14

以下是我第一眼能看到的几个问题。

>

  • 使用try时缺少捕获或最终阻止。
  • 比较字符串和整数(比较前需要一些类型转换)
  • myScanner.toString()不读取用户输入。要读取用户输入,可以使用myScanner.next()。

    里面if条件if(command.equals("HELP"))...,这里你还没有从用户输入中读取命令,看来你想使用if(firstCommand.equals("HELP"))

    代码中从未使用名为“firstNumber”和“secondNumber”的变量。

  •  类似资料: