簡體   English   中英

如何將科學運算符 E 與變量一起使用?

[英]How to use scientific operator E with Variables?

我聲明了一個雙變量如下:

double x=56.27d

然后我試圖做的就是以下內容:(56.27*10*10)

System.out.println(xE2);

這是行不通的。

java中沒有“E”運算符。 這會與變量名稱發生沖突。 double xE2=x*1e2; 在這種情況下,xE2 是一個變量名,但我確實使用 1e2 作為 java 文字。

請按以下步驟操作:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double d = sc.nextDouble(); // Get input from the user
        System.out.println(d * 10 * 10);

        // Displaying it using scientific notation
        // Format the number rounded up to two places after decimal and into scientific notation
        System.out.println(String.format("%.2e", d * 10 * 10));

        // Examples of e or E with double literals
        double x = 1e2; // 1 * 10 to the power of 2 = 100.0
        System.out.println(x);
        double y = 1E2; // 1 * 10 to the power of 2 = 100.0
        System.out.println(y);
        double z = 1e+02; // 1 * 10 to the power of 2 = 100.0
        System.out.println(z);
        System.out.println(d * 1e2);// i.e. d * 100.0
    }
}

示例運行:

Enter a number: 56.27
5627.0
5.63e+03
100.0
100.0
100.0
5627.0

筆記:

  1. 檢查以了解有關Formatter更多信息。
  2. Ee只能用於 double 文字,不能用於 double 變量。

如有任何疑問/問題,請隨時發表評論。

您不應該使用指數表示法,因為它是雙重文字的一部分。 嘗試乘法。

System.out.println(x * 10 * 10)

我建議你閱讀 java.util.Math 庫文檔,它包括許多科學函數,如指數: https : //docs.oracle.com/javase/8/docs/api/java/lang/Math.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM