简单的Java计算器

首先,这不是一个作业问题。我正在练习有关Java的知识。我想一个很好的方法是编写一个没有帮助的简单程序。不幸的是,我的编译器告诉我我不知道如何解决的错误。在不更改大量逻辑和代码的情况下,有人可以指出我的一些错误在哪里吗?谢谢

import java.lang.*;

import java.util.*;

public class Calculator

{

private int solution;

private int x;

private int y;

private char operators;

public Calculator()

{

solution = 0;

Scanner operators = new Scanner(System.in);

Scanner operands = new Scanner(System.in);

}

public int addition(int x, int y)

{

return x + y;

}

public int subtraction(int x, int y)

{

return x - y;

}

public int multiplication(int x, int y)

{

return x * y;

}

public int division(int x, int y)

{

solution = x / y;

return solution;

}

public void main (String[] args)

{

System.out.println("What operation? ('+', '-', '*', '/')");

System.out.println("Insert 2 numbers to be subtracted");

System.out.println("operand 1: ");

x = operands;

System.out.println("operand 2: ");

y = operands.next();

switch(operators)

{

case('+'):

addition(operands);

operands.next();

break;

case('-'):

subtraction(operands);

operands.next();

break;

case('*'):

multiplication(operands);

operands.next();

break;

case('/'):

division(operands);

operands.next();

break;

}

}

}

回答:

package org.com;

import java.lang.*;

import java.util.*;

public class Calculator

{

private int solution;

private static int x;

private static int y;

private char operators;

public Calculator()

{

solution = 0;

Scanner operators = new Scanner(System.in);

Scanner operands = new Scanner(System.in);

}

public int addition(int x, int y)

{

return x + y;

}

public int subtraction(int x, int y)

{

return x - y;

}

public int multiplication(int x, int y)

{

return x * y;

}

public int division(int x, int y)

{

solution = x / y;

return solution;

}

public void calc(int ops){

x = 4;

System.out.println("operand 2: ");

y = 5;

switch(ops)

{

case(1):

System.out.println(addition(x, y));

// operands.next();

break;

case(2):

System.out.println(subtraction(x, y));

// operands.next();

break;

case(3):

System.out.println(multiplication(x, y));

// operands.next();

break;

case(4):

System.out.println(division(x, y));

// operands.next();

break;

}

}

public static void main (String[] args)

{

System.out.println("What operation? ('+', '-', '*', '/')");

System.out.println(" Enter 1 for Addition");

System.out.println(" Enter 2 for Subtraction");

System.out.println(" Enter 3 for Multiplication");

System.out.println(" Enter 4 for Division");

Calculator calc = new Calculator();

calc.calc(1);

}

}

这会工作

以上是 简单的Java计算器 的全部内容, 来源链接: utcz.com/qa/427437.html

回到顶部