当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Double sum()用法及代码示例

Double 类的 sum() 方法返回两个 double 参数的总和,与 '+' 运算符执行的操作相同。

用法:

public static double sum(double a, double b)

参数:

a- 第一个参数通过

b- 第二个参数通过

返回值:

sum() 方法返回 a 和 b 的总和。

例子1

import java.util.Scanner;


public class Double_sumMethodExample1 {

    public static void main(String[] args) {

        Scanner scanner= new Scanner(System.in);

        System.out.print("Enter first number:");

        Float f1=scanner.nextFloat();

        System.out.print("Enter second number:");

        Float f2=scanner.nextFloat();

        Float f3=Float.sum(f1,f2);

        System.out.print("Enter third number:");

        Float f4=scanner.nextFloat();

        System.out.println("Sum = "+Float.sum(f3,f4));

    }

}

输出:

Enter first number:67
Enter second number:98
Enter third number:-98
Sum = 67.0

例子2

import java.util.Scanner;

public class Double_sumMethodExample2 {

    public static void main(String[] args) {

        Scanner scanner= new Scanner(System.in);

        Double a[] =new Double[6];

        System.out.println("Enter your Marks of following subjects");

        System.out.print("English:");

        a[0] = scanner.nextDouble();

        System.out.print("Maths:");

        a[1] = scanner.nextDouble();

        System.out.print("Science:");

        a[2] = scanner.nextDouble();

        System.out.print("S.St:");

        a[3] = scanner.nextDouble();

        System.out.print("Hindi:");

        a[4] = scanner.nextDouble();

        System.out.print("Computer:");

        a[5] = scanner.nextDouble();

        Double d1=0.0;

        for (int i = 0; i < 5; i++) {

            d1 = Double.sum(a[i], a[i + 1]);

            a[i + 1] = d1;

                  }

        Double d2= (d1)/6;

        System.out.println("You got "+d2+"%");

    }}

输出:

Enter your Marks of following subjects
English:87
Maths:98
Science:67
S.St:87
Hindi:56
Computer:45
You got 73.33333333333333%






相关用法

注:本文由纯净天空筛选整理自 Java Double sum() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。