调用类在Java中添加两个数字

时间:2017-03-26 03:59:15

标签: java

我刚开始使用Java,并且不知道如何调用该类并使用正确的变量。我有一个Calculator.java文件,其中我有读取2 int,添加或乘以它们的方法。 (最后粘贴它们)

Calculator.java

class Calculator {

    int x;
    int y;

    public setNumbers(int x, int y) {

        System.out.print("Input the first number: ");
        return x = in.nextInt();
        System.out.print("Input the second number: ");
        return y = in.nextInt();

    }

    public addNumbers(){

        return x + y;

    }

    public multiplyNumbers(){

        return x * y;

    }
}

CalculatorApp.java

class CalculatorApp extends Calculator {

    public int pr() {
        return pr();
    }

    public static void main(String[] args) {

        int choice;

        System.out.println("Please choose one of the following options: \n[1] - Set numbers \n[2] - Add Numbers \n[3] - Multiply numbers \nYour choice is: ");  
        choice = in.nextInt();

        switch (choice) {

            case 1: setNumbers();
                break;

            case 2: addNumbers();
                break;

            case 3: multiplyNumbers();
                break;


        }

    }

}

错误:

 .\Calculator.java:6: error: invalid method declaration; return type required
        public setNumbers(int x, int y) {
               ^
.\Calculator.java:15: error: invalid method declaration; return type required
        public addNumbers(){
               ^
.\Calculator.java:21: error: invalid method declaration; return type required
        public multiplyNumbers(){
               ^
.\Calculator.java:9: error: incompatible types: unexpected return value
                return x = in.nextInt();
                         ^
.\Calculator.java:9: error: cannot find symbol
                return x = in.nextInt();
                           ^
  symbol:   variable in
  location: class Calculator
.\Calculator.java:11: error: incompatible types: unexpected return value
                return y = in.nextInt();
                         ^
.\Calculator.java:11: error: cannot find symbol
                return y = in.nextInt();
                           ^
  symbol:   variable in
  location: class Calculator
.\Calculator.java:17: error: incompatible types: unexpected return value
                return x + y;
                         ^
.\Calculator.java:23: error: incompatible types: unexpected return value
                return x * y;
                         ^
CalculatorApp.java:12: error: cannot find symbol
                choice = in.nextInt();
                         ^
  symbol:   variable in
  location: class CalculatorApp
CalculatorApp.java:16: error: cannot find symbol
                        case 1: setNumbers();
                                ^
  symbol:   method setNumbers()
  location: class CalculatorApp
CalculatorApp.java:19: error: cannot find symbol
                        case 2: addNumbers();
                                ^
  symbol:   method addNumbers()
  location: class CalculatorApp
CalculatorApp.java:22: error: cannot find symbol
                        case 3: multiplyNumbers();
                                ^
  symbol:   method multiplyNumbers()
  location: class CalculatorApp
13 errors

5 个答案:

答案 0 :(得分:0)

java中的每个函数都需要一个返回类型,它是一个方法在返回一个整数值时返回的值,返回类型是int,如果一个floting值则返回类型是float。

另请注意,您已使用in.nextInt(); 这也是错误的你必须使用扫描仪来输入。 此外,setNumbers方法接受两个不需要的参数,因为您只从方法中的用户获取数字。 我认为这些行

public int pr() {
        return pr();
    }

毫无意义。

答案 1 :(得分:0)

我认为在您的Calculator类中,您的函数缺少从函数返回值的返回类型。

你的计算器类可能是这样的。

class Calculator {

  int x;
  int y;

  public void setNumbers() {

    System.out.print("Input the first number: ");
    Scanner in = new Scanner(System.in);
    this.x = in.nextInt();
    System.out.print("Input the second number: ");
    this.y = in.nextInt();

  }

  public int addNumbers(){

    return this.x + this.y;

  }

  public int multiplyNumbers(){

    return this.x * this.y;

  }
}

另外使用" Scanner in = new Scanner(System.in); "在主要功能。

答案 2 :(得分:0)

使用以下代码。如果您希望我解释一些基本的理解,请在下面写下评论。

Calculator.java

public class Calculator {
    int x;
    int y;
    public void setNumbers(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int addNumbers(){
        return x + y;
    }

    public int multiplyNumbers(){
        return x * y;
    }
}

CalculatorApp.java

import java.util.Scanner;
public class CalculatorApp {
    public static void main(String[] args) {
        int choice;
        Scanner in = new Scanner(System.in);
        Calculator calculator = new Calculator();
        while(true) {
            System.out.println("Please choose one of the following options: \n[1] - Set numbers \n[2] - Add Numbers \n[3] - Multiply numbers \n[4] - Exit\nYour choice is: ");  
            choice = in.nextInt();
            switch (choice) {
                case 1:                    
                    System.out.print("\nInput the first number: ");
                    int x = in.nextInt();
                    System.out.print("\nInput the second number: ");
                    int y = in.nextInt();
                    calculator.setNumbers(x, y);
                    break;

                case 2: 
                    int sum = calculator.addNumbers();
                    System.out.println("Sum: " + sum);
                    break;

                case 3:
                    int multiply = calculator.multiplyNumbers();
                    System.out.println("Multiply: " + multiply);
                    break;

                default:
                    System.exit(0);
                }
            }
    }

}

编译 javac Calculator.java ,而不是 javac CalculatorApp.java 。并运行 java CalculatorApp

答案 3 :(得分:0)

所以这里有完整的代码可能有效

class Calculator {

    int x;
    int y;

    public void  setNumbers() {

        System.out.print("Input the first number: ");
        x = in.nextInt();
        System.out.print("Input the second number: ");
       y = in.nextInt();

    }

    public int addNumbers(){

        return x + y;

    }

    public int  multiplyNumbers(){

        return x * y;

    }
}

和班级计算器应用

 class CalculatorApp extends Calculator {

        public int pr() {
            return pr();
        }

        public static void main(String[] args) {

    java.util.Scanner sc =new Scanner(System.in);        
    int choice;

            //note you have to setnumbers before you could add or multiply them
    System.out.println("Press 1 to add numbers");
    Syste.out.print("Press 2 to multiply numbers");

    choice = sc.nextInt();
               setNumbers();
            switch (choice) {

                case 1: 
                system.out.print(  addNumbers());
                    break;

                case 2:
System.out.print( multiplyNumbers());
                    break;

               default:
    S.op.ln("Invalid choice");

            }

        }

    }

答案 4 :(得分:0)

public class Calculator {

    int x;
    int y;

    public void setNumbers() {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Input the first number: ");
        this.x = sc.nextInt();
        System.out.print("Input the second number: ");
        this.y = sc.nextInt();
    }

    public int addNumbers(){

        return this.x + this.y;

    }

    public int multiplyNumbers(){

        return this.x * this.y;

    }
}

public class CalculatorApp{
 public int pr() {
        return pr();
    }

    public static void main(String[] args) {
        Calculator cal = new Calculator();
        int choice;
        Scanner sc = new Scanner(System.in); 
        System.out.println("Please choose one of the following options: \n[1] - Add Numbers \n[2] - Multiply numbers \nYour choice is: ");  
        choice = sc.nextInt();

        switch (choice) {

            case 1: 
                cal.setNumbers();
                System.out.println(cal.addNumbers());
                break;

            case 2: 
                cal.setNumbers();
                System.out.println(cal.multiplyNumbers());
                break;
        }

    }

}