java 如何添加两个或多个数字并显示总和(Netbeans)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25777275/
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-11-02 08:40:23  来源:igfitidea点击:

How to add two or more numbers and display the sum (Netbeans)

javanetbeans

提问by Caam

So i'm a beginner and I am trying to figure out how to add more than 2 numbers and display it through the output of netbeans. Its really confusing for me since i'm still new to programming.

所以我是一个初学者,我想弄清楚如何添加 2 个以上的数字并通过 netbeans 的输出显示它。这对我来说真的很困惑,因为我还是编程新手。

回答by Kick Buttowski

First, you need to define variable.

首先,您需要定义变量。

what is variable?

什么是变量?

A variable provides us with named storage that our programs can manipulate.

变量为我们提供了程序可以操作的命名存储。

There are different type variable in Java like int, double, char, String, and so on.

Java 中有不同的类型变量,如intdoublecharString等。

What do I mean by type variable?

类型变量是什么意思?

  1. determines the size and layout of the variable's memory
  2. he range of values that can be stored within that memory
  3. the set of operations that can be applied to the variable
  1. 确定变量内存的大小和布局
  2. 可以存储在该内存中的值范围
  3. 可以应用于变量的一组操作

How to define Variable in Java

如何在Java中定义变量

data type variable [ = value][, variable [= value] ...] ;

数据类型变量[=值][,变量[=值]...];

For example , you can define two variable as int type as follows

例如,您可以将两个变量定义为 int 类型,如下所示

int firstNumber = 1;
int secondNumber = 2;

Note if you like you can define more than two variables by following up the rules.

请注意,如果您愿意,您可以按照规则定义两个以上的变量。

you can do Arithmetic Operators on your variables

您可以对变量进行算术运算符

Operator    Description
  +         Additive operator (also used for String concatenation)
  -         Subtraction operator
  *         Multiplication operator
  /         Division operator
  %        Remainder operator

Here you want to add so you need to use +operator.

在这里你要添加所以你需要使用+运算符。

read this which is my source : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

阅读这是我的来源:http: //docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

At the end, you should define a variable that carries the summation of two variables like

最后,您应该定义一个变量,该变量携带两个变量的总和,例如

int sum = number1 + number2;

and print the value on your console by using System.out.print/ln

并使用 System.out.print/ln

System.out.print("the summation of number one and number two is " + sum);

Source:

来源:

  1. http://www.homeandlearn.co.uk/java/java_int_variables.html
  2. http://www.tutorialspoint.com/java/java_variable_types.htm
  1. http://www.homeandlearn.co.uk/java/java_int_variables.html
  2. http://www.tutorialspoint.com/java/java_variable_types.htm

回答by Wyzard

int a = 1;
int b = 2;
int c = 3;
int d = a + b + c;
System.out.println(d);

If you want to add up the numbers in an array or collection:

如果要将数组或集合中的数字相加:

int[] numbers = { 1, 2, 3 };
int sum = 0;
for (int number : numbers) {
  sum += number;
}
System.out.println(sum);