當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。