1

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

10 回答 10

4

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

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

于 2010-04-29T01:31:57.067 回答
4
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);


  } 
} 

这将起作用

于 2010-04-29T08:05:48.203 回答
2

另一个问题是,线路

y = operands.next();

正在尝试将 aString返回的 from放入声明为 typeScanner.next()的 a 变量中。yint

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

于 2010-04-29T01:32:49.437 回答
2
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);
    }
}
于 2010-04-29T10:35:34.493 回答
1

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

于 2010-04-29T01:35:48.223 回答
1

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

希望这可以帮助!

于 2012-12-23T00:16:17.193 回答
0

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

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

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

public int addition(int x, int y);

不能用 调用addition(operands),即只有一个参数,并且参数类型错误(该方法需要两个int,你给它一个Scanner)。所有这些方法也是如此。您需要intScanner. 你可以用Scanner.nextInt().

于 2010-04-29T01:32:43.837 回答
-1
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;
       }
  }
}
于 2013-06-01T14:10:28.970 回答
-1

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

于 2015-09-30T21:44:32.673 回答
-2

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

导入 java.util.*;
进入你的程序,因为它使程序不必要地大而慢。你需要做的就是
导入 java.util.Scanner;
如果我是正确的,java.lang 中的大部分内容(如果不是所有内容)都已经为您导入了。

于 2010-04-29T06:39:09.930 回答