使用函数的基本Java计算器(初学者)

时间:2017-05-07 04:19:16

标签: java calculator

嘿伙计们,我还是java的初学者还在学习。我对功能有疑问。所以我创造的只是为了一个有趣的基本计算器

package calculator;
import java.util.Scanner;

public class Calculator {

 public void printMenu() {
    System.out.println("Basic Calculator");
    System.out.println("A. Addition");
    System.out.println("B. Subtraction");
    System.out.println("C. Multiplication");
    System.out.println("D. Division");
    System.out.println("Enter letter of operation");
}

public void enterNumbers() {
    System.out.println("Enter first number:");
    int n1 = scan.nextInt();
    System.out.println("Enter second number:");
    int n2 = scan.nextInt();
}

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Calculator calc = new Calculator();
    calc.printMenu();
    String choice = scanner.nextLine();
    Calculator operation = new Calculator();

    switch (choice) {
        case "a":
        case "A":
            System.out.println("Addition");
            operation.enterNumbers();
    }
  }
}

但是当我进行测试时,当我继续进行案例时,这是我的补充,它显示了这一点:

run:
Basic Calculator
A. Addition
B. Subtraction
C. Multiplication
D. Division
Enter letter of operation: 
A
Addition
Enter first number:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at calculator.scan.nextInt(scan.java:19)
    at calculator.Calculator.enterNumbers(Calculator.java:29)
    at calculator.Calculator.main(Calculator.java:46)
/Users/juancarlosraymundo/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)

当我调用函数enterNumbers时是否有错误?非常感谢你,我希望了解更多。

5 个答案:

答案 0 :(得分:0)

扫描仪必须是一个字段,扫描不存在

我在类线下定义扫描仪 并在scann方法中将scanner更改为enterNumbers()

package calculator;

import java.util.Scanner;

/** * * @author juancarlosraymundo */ 
public class Calculator {
Scanner scanner = new Scanner(System.in);

public void printMenu() {
    System.out.println("Basic Calculator");
    System.out.println("A. Addition");
    System.out.println("B. Subtraction");
    System.out.println("C. Multiplication");
    System.out.println("D. Division");
    System.out.println("Enter letter of operation");
}

public void enterNumbers() {
    System.out.println("Enter first number:");
    int n1 = scanner .nextInt();
    System.out.println("Enter second number:");
    int n2 = scanner .nextInt();

}

public static void main(String[] args) {
    Calculator calc = new Calculator();
    calc.printMenu();
    String choice = scanner.nextLine();
    Calculator operation = new Calculator();

    switch (choice) {
        case "a":
        case "A":
            System.out.println("Addition");
            operation.enterNumbers();
    }
}
}

答案 1 :(得分:0)

您似乎忘记了scan的声明。

 Scanner scan = new Scanner(System.in);
public class Calculator正下方的

可以完成工作

答案 2 :(得分:0)

在您的enterNumbers()函数中,scan是一个未知的对象引用:

public void enterNumbers() {
    System.out.println("Enter first number:");
    int n1 = scan.nextInt();
    System.out.println("Enter second number:");
    int n2 = scan.nextInt();

}

在上面的函数中创建scanner对象或将其作为参数传递。

答案 3 :(得分:0)

正如其他人所指出的那样:代码中的问题是您是一个名为 scan 的变量,未在源代码中定义。

因此,直接的答案是让 scanner 成为你班级的一个领域,并在你需要用户输入时使用它(你必须避免在系统上使用多个扫描仪。在btw中) 。

但真正的答案是:当你的课程编译错误时,修复他们。运行不完全编译的代码会导致严重的问题。有时,像eclipse这样的IDE允许运行不完整的代码,但是当你是新手时避免这样做。

答案 4 :(得分:0)

public class Calculator {
static Scanner scanner = new Scanner(System.in);

public void printMenu() {
    System.out.println("Basic Calculator");
    System.out.println("A. Addition");
    System.out.println("B. Subtraction");
    System.out.println("C. Multiplication");
    System.out.println("D. Division");
    System.out.println("Enter letter of operation");
}

public void enterNumbers() {
    System.out.println("Enter first number:");
    int n1 = scanner.nextInt();
    System.out.println("Enter second number:");
    int n2 = scanner.nextInt();

}

public static void main(String[] args) {

    Calculator calc = new Calculator();
    calc.printMenu();
    char choice = scanner.next().charAt(0);//switch cant work with string //,declare choice as character
    Calculator operation = new Calculator();

    switch (choice) {//switch cant work with string 
    case 'a':
    case 'A':
        System.out.println("Addition");
        operation.enterNumbers();

    }

}

}