java 简单的命令行计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6772222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:12:05  来源:igfitidea点击:

Simple Command Line calculator

java

提问by Cipher

I have written a code for a command-line calculator in Java but I can't understand the errors which are not allowing it to compile

我用 Java 为命令行计算器编写了代码,但我无法理解不允许它编译的错误

public class Calculator{
public static void main(String[] args){

int numOne = args[0];
int numTwo = args[1];

add(numOne, numTwo) {
    result = numOne + numTwo;
    System.out.println("{numOne} + {numTwo} = {result}");
}

subtract(numOne, numTwo) {
    result = numOne - numTwo;
    System.out.println("{numOne} - {numTwo} = {result}");
}

multiply(numOne, numTwo) {
    result = numOne * numTwo;
    System.out.println("{numOne} * {numTwo} = {result}");
}

divide(numOne, numTwo) {
    result = numOne / numTwo;
    System.out.println("{numOne} / {numTwo} = {result}");
}
}
}

回答by vhtellez

The translation to java of your code is:

将您的代码翻译成 java 是:

public class Calculator {

  public static void add(int numOne, int numTwo) {
    int result = numOne + numTwo;
    System.out.println(numOne + " + " + numTwo + " = " + result);
  }

  public static void subtract(int numOne, int numTwo) {
    int result = numOne - numTwo;
    System.out.println(numOne + " - " + numTwo + " = " + result);
  }

  public static void multiply(int numOne, int numTwo) {
    int result = numOne * numTwo;
    System.out.println(numOne + " * " + numTwo + " = " + result);
  }

  public static void divide(int numOne, int numTwo) {
    int result = numOne / numTwo;
    System.out.println(numOne + " / " + numTwo + " = " + result);
  }

  public static void main(String[] args) {
    int numOne = Integer.parseInt(args[0]);
    int numTwo = Integer.parseInt(args[1]);
    add(numOne, numTwo);
    subtract(numOne, numTwo);
    multiply(numOne, numTwo);
    divide(numOne, numTwo);
  }
}

回答by Thorbj?rn Ravn Andersen

You cannot define methods inside your main method, so this is most likely one of your first programs in Java. Nothing wrong with that, but this makes you an example of the target audience for most introductory texts for programming in Java (if you do not have a teacher).

您不能在 main 方法中定义方法,因此这很可能是您使用 Java 编写的第一个程序。这没什么不对,但这使您成为大多数 Java 编程介绍性文本的目标受众示例(如果您没有老师)。

The Oracle Java Tutorial is quite nice. You may want to start at http://download.oracle.com/javase/tutorial/java/index.html

Oracle Java 教程非常好。您可能想从http://download.oracle.com/javase/tutorial/java/index.html开始

回答by SJuan76

args[] is a String[]. You can not assign its members directly into int.

args[] 是一个字符串[]。您不能将其成员直接分配到 int 中。

You should use Integer.parseInt(String) to get its representation as int.

您应该使用 Integer.parseInt(String) 将其表示为 int。

Also. there is no "println()". You probably mean System.out.println(String). And in the parameters there is no "field sustitution" (as is C printf), but you need to mount the expression:

还。没有“println()”。您可能是指 System.out.println(String)。并且在参数中没有“字段替换”(就像 C printf 一样),但是您需要安装表达式:

 System.out.println(numOne + " / " + numTwo + " = " + result);

(here + is the concatenation operator, as it involves Strings)

(这里 + 是连接运算符,因为它涉及字符串)

Last (that I can see at first glance), you must define the functions that you call outside of main (but inside a class). Also you need either to declare them static (as main) or create a new object of this class in order to access them from main().

最后(乍一看),您必须定义在 main 之外(但在类内)调用的函数。您还需要将它们声明为静态(作为 main)或创建此类的新对象,以便从 main() 访问它们。

If after all of that it still does not compile, post the compile error messages.

如果在所有这些之后它仍然无法编译,请发布编译错误消息。