在课堂外调用变量

时间:2016-12-14 19:50:21

标签: java variables methods scope

我正在尝试调用三个超出我方法范围的变量。 Application类调用Calculator类来执行基本操作。

我知道有几种方法可以做到这一点,但是调用变量操作和方法addNumberssubtractNumbersmultiplyNumbers和{{1}的最基本方法是什么来自divideNumbers类中的calculate()方法?

Application类的代码:

Calcuator

Calculator类的代码:

import java.util.Scanner;

public class Application {
  public static void main(String[] args){
    int quantity1;
    int quantity2;
    int operation;
    Scanner userInput = new Scanner(System.in);

    System.out.println("Please select one of the following operations: ");
    System.out.println("1- Addition, 2-Subtraction, 3-Multiplication, 4-Division, 5-exit: ");
    operation = userInput.nextInt();
    System.out.println("Please enter the first quantity: ");
    quantity1 = userInput.nextInt();

    System.out.println("Please enter the second quantity: ");
    quantity2 = userInput.nextInt();
    Calculator myCalculator = new Calculator();
    myCalculator.calculate();
  }

  public void calculate () {
    switch(operation) {
      case 1:
        System.out.println(addNumbers(quantity1, quantity2));
      case 2:
        System.out.println(subtractNumbers(quantity1, quantity2));
      case 3:
        System.out.println(multiplyNumbers(quantity1, quantity2));
      case 4:
        System.out.println(divideNumbers(quantity1, quantity2));
      case 5:
        System.exit(0); 
    }    
  }
}

*编辑:我需要从Application类调用calculate方法。

4 个答案:

答案 0 :(得分:1)

我完全不明白你为什么要像现在这样构建你的代码。从我的角度来看,主应用程序负责读取三个输入值(两个操作数和一个操作符)。它还可以包含计算器的实例。

假设计算方法移动到计算器本身并将这三个值作为参数。然后计算器可以执行操作(基于操作符/开关)语句并返回结果。

当您现在将主应用程序中的三个输入传递给Calculator类时,这应该可以解决您的问题。

答案 1 :(得分:0)

package com.company;

import java.util.Scanner;

public class Application {
        private static int quantity1;
        private static int quantity2;
        private static int operation;
        private Calculator myCalculator = new Calculator();

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);

        System.out.println("Please select one of the following operations: ");
        System.out.println("1- Addition, 2-Subtraction, 3-Multiplication, 4-Division, 5-exit: ");
        operation = userInput.nextInt();
        System.out.println("Please enter the first quantity: ");
        quantity1 = userInput.nextInt();

        System.out.println("Please enter the second quantity: ");
        quantity2 = userInput.nextInt();


        new Application().calculate();
    }

    public void calculate() {

        switch (operation) {
            case 1:

                System.out.println(myCalculator.addNumbers(quantity1, quantity2));

            case 2:
                System.out.println(myCalculator.subtractNumbers(quantity1, quantity2));

            case 3:
                System.out.println(myCalculator.multiplyNumbers(quantity1, quantity2));

            case 4:
                System.out.println(myCalculator.divideNumbers(quantity1, quantity2));

            case 5:
                System.exit(0);
        }

    }
}
  • 将您的本地变量quantity1,quantity2和操作移到main()之外,并将它们设为静态变量
  • 也将myCalculator移到了外面。
  • myCalculator
  • 调用addNumbers,substractNumbers
  • 没有改变课程Calculator
  • 中的任何内容

这是我的输出:

Please select one of the following operations: 
1- Addition, 2-Subtraction, 3-Multiplication, 4-Division, 5-exit: 
1
Please enter the first quantity: 
2
Please enter the second quantity: 
2
4
0
4
1

<强>注意: 您的switch statement无效,它会打印所有操作结果,因为您没有为任何情况提供任何break,这就是为什么所有操作都已显示,它被称为blocks fall through。你需要做你唯一的工作来修复。这是一个enter link description here可以帮助你

答案 2 :(得分:0)

我认为calculate()方法应该在Calculator类而不是Application类中,根据代码片段中的问题。

使用实例变量而不是方法局部变量并将它们标记为私有(如果从静态方法访问,则也是静态的)。这样,您可以使用适当的setter方法设置值,并使用Calculator类中相应的getter方法获取,因为它们是公共方法。

这是一种标准做法,称为JavaBean约定。

答案 3 :(得分:0)

基本而优雅的方法是在Calculator类中编写构造函数。

  • 在main方法中,只需在实例化新对象operation时将参数quantity1quantity2myCalculator传递给构造函数。
  • 而不是将calculate()方法放在Application课程中,并且如您的说明中所述,calculate()方法应该放在Calculator课程中。
  • 调用calculate()
  • 中声明的Calculator方法

main(String[] args)方法的片段

Calculator myCalculator = new Calculator(operation, quantity1, quantity2);
myCalculator.calculate();

Calculator类的代码:

package calculator;
import java.util.Scanner;

public class Calculator {
    // Add three private variable to indicate operation and operands
    private int operand1;
    private int operand2;
    private int operation;

    // You should write a constructor to take parameters read from Scanner
    public Calculator(int operation, int quantity1, int quantity2){
        operand1 = quantity1;
        operand2 = quantity2;
        this.operation = operation;
    }

    public int addNumbers(int A, int B){
        return A+B;
    }
    public int subtractNumbers(int A, int B){
        return A - B;
    }
    public int multiplyNumbers(int A, int B){
        return A * B;
    }
    public int divideNumbers(int A, int B){
        return A / B;
    }

    // Your calculator method should goes here
    public void calculate(){
        switch(operation){
        case 1:
            System.out.println(addNumbers(operand1, operand2));
            break;
        case 2:
             System.out.println(subtractNumbers(operand1, operand2));
            break;
        case 3:
            System.out.println(multiplyNumbers(operand1, operand2));
            break;
        case 4:
            System.out.println(divideNumbers(operand1, operand2));
            break;
        default:
            break;
        }
    }
}