Как исправить ошибку в калькуляторе Java, который является lab2 в курсе развития Android Udacity? - PullRequest
0 голосов
/ 06 ноября 2019

Я пытался запустить этот код, но он не работает.

Вы должны создать калькулятор с его основными функциями.

Создать класс калькулятора, в котором есть сумма, вычитание, деление иметоды умножения, все они принимают два параметра типа int и возвращают полученное значение типа int.

Пример. Когда метод sum вызывается как sum (5,6), он должен возвращать 11, которое является суммой 5 и 6.

Как я могу это исправить?

class MyCalculator {
    int input1 = 15;
    int input2 = 5; 
    public float sum() {

        int sum = input1 + input2;
         return sum;
    }

    public float divid() {
        // TODO: write java code to calculate the average for all input variables
       int divid = (input1 / input2);
         return divid;
    }
    public float subtract() {
        // TODO: write java code to calculate the average for all input variables
        int subtract = (input1 - input2);
         return subtract;
    }
    public float multiply() {
        // TODO: write java code to calculate the average for all input variables
        int multiply = (input1 * input2);
         return multiply;
    }

}

Это дает мне эту ошибку

method sum in class MyCalculator cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
method divid in class MyCalculator cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
method subtract in class MyCalculator cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
method multiply in class MyCalculator cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length

1 Ответ

0 голосов
/ 06 ноября 2019

В каждом объявлении функции тип имеет тип «float», но вы возвращаете «int». Так что лучше обновлять оба элемента как одинаковые. Ниже приведены изменения кода

public float sum() {

        float sum = input1 + input2;
         return sum;
    }

    public float divid() {
        // TODO: write java code to calculate the average for all input variables
       float divid = (input1 / input2);
         return divid;
    }
    public float subtract() {
        // TODO: write java code to calculate the average for all input variables
        float subtract = (input1 - input2);
         return subtract;
    }
    public float multiply() {
        // TODO: write java code to calculate the average for all input variables
        float multiply = (input1 * input2);
         return multiply;
    }

Затем попробуйте

...