Java Float.sum()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-float-sum-method
Java sum()
方法是java.lang
包的Float
类的一部分。该方法返回作为参数传递的浮点值的数值总和(即根据+
运算符简单地将作为参数传递的两个数字相加)。
语法:
public static float sum(float a, float b)
参数:
传递的参数包括两个浮点值,其相加将被返回。
返回:
返回作为参数传递的两个浮点值之和。
例 1:
这里,为了更好地理解该方法,取一些正数和负数。
import java.lang.Float;
public class StudyTonight
{
public static void main(String[] args)
{
float a = 10.84f;
float b = 29.132f;
float c = -67.99f;
System.out.println("The sum of a and b is = " + Float.sum(a, b)); // It will return the sum of a and b
System.out.println("The sum of a and b is = " + Float.sum(b, c)); // It will return the sum of a and b
}
}
a 和 b 之和为= 39.972 a 和 b 之和为= -38.857998
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the two values : ");
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
float b = sc.nextFloat();
System.out.println("sum is = " + Float.sum(a, b)); // will return the sum of a and b.
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
输入两个值: 89.56 42.789 之和为= 132.349
- T4】输入两个值: -56.94 89.43 之和为= 32.49
- *输入两个值: 90 0x598 无效输入
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。