【问题标题】:Simple Java calculator简单的 Java 计算器
【发布时间】:2011-02-13 14:56:06
【问题描述】:

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

【问题讨论】:

  • 你遇到什么样的错误?
  • 感谢您发布代码。但是,当您发布从编译器获得的错误消息的文本时,它也有很大帮助 - 这使人们更容易快速识别问题(无需阅读整个代码或自己编译)。

标签: java calculator


【解决方案1】:

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

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

【讨论】:

  • 您是否建议在 main 中重新声明它们,例如:this.operands; this.operators;还是出于其他目的?
  • @Kevin Duke:您不能在运行时使用该语言向类添加字段。您需要在计算器中指定它们,就像您指定解决方案和运算符一样。 Main 也没有“this”,因为它必须是静态的。
【解决方案2】:

你的 main 方法需要这样声明:

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

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

public int addition(int x, int y);

不能用addition(operands)调用,即只有一个参数,而且参数类型错误(该方法需要两个int,你给它一个Scanner)。所有这些方法也是如此。您需要从Scanner 中提取ints。你可以通过Scanner.nextInt() 做到这一点。

【讨论】:

    【解决方案3】:

    另一个问题是,这条线

    y = operands.next();
    

    正在尝试将从Scanner.next() 返回的String 放入声明为类型int 的变量y

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        import java.util.*;
        到您的程序中,因为它会使程序变得不必要地大而慢。为此,您只需要
        import java.util.Scanner;
        如果我是正确的,java.lang 中的大多数(如果不是)所有内容都已经为您导入了。

        【讨论】:

        • 这是错误的。导入通配符包对运行时性能(或程序的大小,就此而言)没有影响。它仅用于编译时符号解析。
        【解决方案6】:
        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);
        
        
          } 
        } 
        

        这会起作用

        【讨论】:

          【解决方案7】:
          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);
              }
          }
          

          【讨论】:

            【解决方案8】:

            这一切都很好,但是你用什么程序来编写你的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 会大大减慢您的程序。

            希望这会有所帮助!

            【讨论】:

            • 你最后一点是错误的。导入通配符包对运行时性能没有影响。它仅用于编译时符号解析
            • 很抱歉。只是我听到很多人说导入整个 java 库会降低程序的速度。还是我听错了?
            • 我怀疑你没有听错——周围有很多错误信息。看看this question
            【解决方案9】:
            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;
                   }
              }
            }
            

            【讨论】:

              【解决方案10】:

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

              【讨论】:

                猜你喜欢
                • 2013-06-27
                • 2013-11-29
                • 2013-12-31
                • 1970-01-01
                • 2011-05-16
                • 2016-05-14
                • 1970-01-01
                • 2010-10-13
                • 1970-01-01
                相关资源
                最近更新 更多