我正在用Java构建计算器,并且在执行运算符时遇到问题

问题描述 投票:0回答:2

我对Java还是很陌生,以前从未做过编程。尝试为每个运算符运行代码时出现以下错误。我希望它以字符串形式接受用户输入,但返回一个双精度值作为结果。我知道可能会有几个错误,但是可以提供任何帮助。

类型不匹配:无法从字符串转换为双精度类型不匹配:无法从int转换为String未为参数类型java.lang.String,java.lang.String

定义运算符-

这是我的代码

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

public class Calculator {

    public static void main(String[] args) {
        String ADD = "+";
        String SUB = "-";
        String DIV = "/";
        String MUL = "*";
        String MOD = "%";
        String POW = "^";


        Scanner myScanner = new Scanner(System.in);
        boolean continueLoop = true;

        try {

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

            System.out.print("Welcome to my Calculator! Lets Begin!" + "\n");

            System.out.print("Type HELP or START to begin: ");

            String firstCommand = myScanner.nextLine();

            if (command.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());

            System.out.println("Please Enter your First number: ");

            x = myScanner.toString();

            Double secondNumber = Double.parseDouble(myScanner.nextLine());

            System.out.println("Please Enter your Second Number: ");

            y = myScanner.toString();

            Double converted = Double.parseDouble(myScanner.nextLine());    

            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.next();

            switch(command) {

            case 1:
                result = (x+y); break;

            case 2:
                result = (x-y); break;

            case 3:
                result = (x*y); break;

            case 4:
                result = (x/y); break;

            case 5:
                result = (x%y); break;

            case 6:
                result = (Math.PI);

            case 7:
                result = (Math.pow(x, y));
            }


            if(command. >= 1 && command <= 5)
                System.out.println("Your answer is: " + result);
java double calculator
2个回答
0
投票
public static void main(String[] args) {

    Scanner myScanner = new Scanner(System.in);

    try {

        String x, y;
        // int x,y;
        double result = 0;

        int command1;

        System.out.print("Welcome to my Calculator! Lets Begin!" + "\n");

        System.out.print("Type HELP or START to begin: ");

        String firstCommand = myScanner.nextLine();

        if (firstCommand.equals("HELP") || firstCommand.equals("START")) {
            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.println("Please Enter your First number: ");

        x = myScanner.nextLine();

        Double firstNumber = Double.parseDouble(x);

        System.out.println("Please Enter your Second Number: ");

        y = myScanner.nextLine();

        Double secondNumber = Double.parseDouble(y);

        System.out.print("\n1: Add. \n2: Sub. \n3: Mult. \n4: Divide. \n5: Modulo. \n6: Pi \n7: Pow.");
        System.out.print("\nEnter your operator: ");

        command1 = myScanner.nextInt();

        switch (command1) {

        case 1:
            result = (firstNumber + secondNumber);
            break;

        case 2:
            result = (firstNumber - secondNumber);
            break;

        case 3:
            result = (firstNumber * secondNumber);
            break;

        case 4:
            result = (firstNumber / secondNumber);
            break;

        case 5:
            result = (firstNumber % secondNumber);
            break;

        case 6:
            result = (Math.PI);

        case 7:
            result = (Math.pow(firstNumber, secondNumber));
        }

        if (command1 >= 1 && command1 <= 5)
            System.out.println("Your answer is: " + result);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        myScanner.close();
    }
}

尝试使用此代码。


0
投票

下面是一些我乍看之下的问题。

  1. 使用try时没有捕获或最终阻止。
  2. 比较字符串和整数(比较之前需要进行一些类型转换)
  3. myScanner.toString()不读取用户输入。要读取用户输入,您可以使用myScanner.next()。
  4. 如果条件为if (command.equals("HELP"))...,在这里您还没有从用户输入中读取命令,似乎您想使用if (firstCommand.equals("HELP"))。5.代码中从未使用过的变量名为“ firstNumber”和“ secondNumber”。
  5. 提示用户提供输入之前,请使用myScanner.nextLine()
© www.soinside.com 2019 - 2024. All rights reserved.