简单的Java计算器

发布于 2024-08-30 15:57:52 字数 1570 浏览 4 评论 0 原文

首先,这不是一个家庭作业问题。我正在练习我的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;
       }
  }
}

Firstly this is not a homework question. I am practicing my knowledge on java. I figured a good way to do this is to write a simple program without help. Unfortunately, my compiler is telling me errors I don't know how to fix. Without changing much logic and code, could someone kindly point out where some of my errors are? Thanks

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;
       }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

源来凯始玺欢你 2024-09-06 15:57:53

这一切都很棒,但是你用什么程序来编写你的java呢?
也许您应该考虑使用像 Eclipse 这样的 IDE,因为它可以自动检测错误并添加导入。 (我不确定你的是否这样做)它还告诉你你的程序的问题是什么“英语”。
另外,考虑这个类可能是一种更简单、更简单的计算器方法:

public class Calculator {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter an Operator: ");
    String in = sc.next();
    char oper = in.charAt(0);

    System.out.print("Enter a number: ");
    in = sc.next();
    double num1 = Double.parseDouble(in);

    System.out.print("Enter another number: ");
    in = sc.next();
    double num2 = Double.parseDouble(in);

    if(oper == '+') {
        double result = num1 + num2;
        System.out.println(result);
    } else if(oper == '-') {
        double result = num1 - num2;
        System.out.println(result);
    } else if(oper == 'x') {
        double result = num1 * num2;
        System.out.println(result);
    } else if(oper == '/') {
        double result = num1 / num2;
        System.out.println(result);
    } else {
        double result = num1 % num2;
        System.out.println(result);
    }
        System.out.println("Hope this helped your mathmatical troubles!");
}

}
作为一个习惯问题,与其这样做:

import java.util.*;

不如这样做:

import java.util.Scanner;

这在这里可能没有多大区别,但是如果您正在运行一个更大的程序,导入整个 java.util 将大大减慢您的速度程序。

希望这有帮助!

This is all great, but what program are you using to write your java?
Maybe you should consider using an IDE like Eclipse, as it can detect errors automatically and also adds imports. (I'm not sure if yours does this) It also tells you what the problem with your program is 'in english'.
Also, consider this class as maybe an easier and less complicated way of doing a calculator:

public class Calculator {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter an Operator: ");
    String in = sc.next();
    char oper = in.charAt(0);

    System.out.print("Enter a number: ");
    in = sc.next();
    double num1 = Double.parseDouble(in);

    System.out.print("Enter another number: ");
    in = sc.next();
    double num2 = Double.parseDouble(in);

    if(oper == '+') {
        double result = num1 + num2;
        System.out.println(result);
    } else if(oper == '-') {
        double result = num1 - num2;
        System.out.println(result);
    } else if(oper == 'x') {
        double result = num1 * num2;
        System.out.println(result);
    } else if(oper == '/') {
        double result = num1 / num2;
        System.out.println(result);
    } else {
        double result = num1 % num2;
        System.out.println(result);
    }
        System.out.println("Hope this helped your mathmatical troubles!");
}

}
And as a matter of habit, instead of doing:

import java.util.*;

it is better to do:

import java.util.Scanner;

This probably doesn't make much of a difference here, but if you are running a much bigger program importing the whole of java.util will considerably slow down your program.

Hope this helps!

避讳 2024-09-06 15:57:53

您的主要方法需要这样声明:

public static void main(String[] args) {..}

此外,您似乎只为所有算术方法(加法、减法等)提供一个参数,尽管它们需要两个。

public int addition(int x, int y);

不能用addition(operands)调用,即只有一个参数,并且参数的类型错误(该方法需要两个int,你给它一个<代码>扫描仪)。所有这些方法都是如此。您需要从 Scanner 中提取 int。您可以使用 Scanner.nextInt() 来做到这一点。

Your main method needs to be declared like this:

public static void main(String[] args) {..}

Furthermore, it seems like you are only supplying one argument to your all your arithmetic methods (addition, subtraction etc), although they require two.

public int addition(int x, int y);

Can not be called with addition(operands), that is only one argument, and the argument is of the wrong type (the method needs two int, you give it a Scanner). The same goes for all those methods. You need to extract the ints from the Scanner. You can do that with Scanner.nextInt().

梦中的蝴蝶 2024-09-06 15:57:53
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;
       }
  }
}
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;
       }
  }
}
别念他 2024-09-06 15:57:53

您要求用户输入整数,但您将语句 operands.next(); 作为输入。尝试与变量和用户输入保持一致,因此将其更改为 operands.nextInt() 会有所帮助。

You are asking for the user to type in integers, but you put the statement operands.next(); as the input. Try to keep consistent with your variables and user input, so changing it to operands.nextInt() would help.

韶华倾负 2024-09-06 15:57:53

作为提示,开始投掷通常不是一个好主意

import java.util.*;

into your program because it makes the program unnecessarily large and slow. All you need for this is to

import java.util.Scanner;

If I'm correct, most if not everything in java.lang is already imported for you.

Just as a tip, it's generally not a good idea to start throwing

import java.util.*;

into your program because it makes the program unnecessarily large and slow. All you need for this is to

import java.util.Scanner;

If I'm correct, most if not everything in java.lang is already imported for you.

月下凄凉 2024-09-06 15:57:52
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);


  } 
} 

这会起作用

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);


  } 
} 

This will work

你是年少的欢喜 2024-09-06 15:57:52

操作数运算符超出了main的范围。您在构造函数中声明局部变量,因此当您退出构造函数时,它们有资格进行 GC 并且消失。

您有编译错误 - 其中 10 个。

operands and operators are out of scope for main. You declare local variables in the constructor, so when you exit the ctor they're eligible for GC and gone.

You have compilation errors - 10 of them.

乙白 2024-09-06 15:57:52

另一个问题是,该行

y = operands.next();

尝试放置从 String。 html#next()" rel="nofollow noreferrer">Scanner.next() 到声明为类型 的变量 y 中int

Scanner.nextInt( ) 方法可用于尝试返回 int

Another issue is, the line

y = operands.next();

is attempting to place a String returned from Scanner.next() into the a variable y which is declared as a type int.

The Scanner.nextInt() method can be used to attempt to return an int.

七七 2024-09-06 15:57:52
package com.abc;

import java.util.Scanner;

public class Calculator {
    private static final String pos = "+";
    private static final String neg = "-";
    private static final String mult = "*";
    private static final String div = "/";

    private enum operation {
        pos, neg, mult, div
    };
    private int solution;
    private int x;
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private int y;



    static Scanner operators;

    public Calculator() {
        solution = 0;
        operators = 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 static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println("Insert 2 numbers");

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

        calc.setX(Integer.parseInt(operators.next()));

        System.out.println("operand 2: ");
        calc.setY(Integer.parseInt(operators.next()));

        System.out.println("What operation? ('pos', 'neg', 'mult', 'div')");
        operation ttt = operation.valueOf(operators.next());
        int output = 0 ;
        switch(ttt){
        case pos:
            output = calc.addition(calc.getX(), calc.getY());

            break;
          case neg:
              output = calc.subtraction(calc.getX(), calc.getY());

            break;
          case mult:
              output = calc.multiplication(calc.getX(), calc.getY());

            break;
          case div:
              output = calc.division(calc.getX(), calc.getY());

            break;
        }
        System.out.println("output ="+output);
    }
}
package com.abc;

import java.util.Scanner;

public class Calculator {
    private static final String pos = "+";
    private static final String neg = "-";
    private static final String mult = "*";
    private static final String div = "/";

    private enum operation {
        pos, neg, mult, div
    };
    private int solution;
    private int x;
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private int y;



    static Scanner operators;

    public Calculator() {
        solution = 0;
        operators = 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 static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println("Insert 2 numbers");

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

        calc.setX(Integer.parseInt(operators.next()));

        System.out.println("operand 2: ");
        calc.setY(Integer.parseInt(operators.next()));

        System.out.println("What operation? ('pos', 'neg', 'mult', 'div')");
        operation ttt = operation.valueOf(operators.next());
        int output = 0 ;
        switch(ttt){
        case pos:
            output = calc.addition(calc.getX(), calc.getY());

            break;
          case neg:
              output = calc.subtraction(calc.getX(), calc.getY());

            break;
          case mult:
              output = calc.multiplication(calc.getX(), calc.getY());

            break;
          case div:
              output = calc.division(calc.getX(), calc.getY());

            break;
        }
        System.out.println("output ="+output);
    }
}
不羁少年 2024-09-06 15:57:52

除了其他答案之外,您的 main() 方法必须是静态的才能成为程序入口点。在 main() 中,您需要构造自己的 Calculator 对象,并调用该对象的方法。

In addition to the other answers, your main() method must be static in order to be a program entry point. In main() you will need to construct your own Calculator object, and call methods on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文