⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 volumes.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 JAVA
字号:
//Chapter 2, Exercise 4 

public class Volumes {
  public static void main(String args[]) {
    double sunRad = 865000.0/2.0;   // Sun radius in miles is half the diameter
    double earthRad = 7600.0/2.0;   // Earth radius likewise
    double fourOverThree = 4.0/3.0; // A convenient constant, 4/3
    double sunVol = 0;
    double earthVol = 0; 
    double ratioVol = 0;             

//    You can declare several variables of the same type in a single statement if you wish,
//    so we could write the definitions as:
    
//   double sunRad = 865000.0/2.0, earthRad = 7600.0/2.0,
//   double fourOverThree = 4.0/3.0; 
//   double sunVol = 0, earthVol = 0, ratioVol = 0;             
   
//   However, except in trivial cases it is better to declare variables in separate statements
//   as this is clearer and reduces the possibility of errors.
   

    // Find the volumes of earth and sun:
    earthVol = fourOverThree*Math.PI*Math.pow(earthRad,3);
    sunVol = fourOverThree*Math.PI*Math.pow(sunRad,3);
    
    // Find the ratio of their volumes:
    ratioVol = sunVol/earthVol;

    // Output the results:
    System.out.println("Volume of the earth is " + earthVol + " cubic miles");
    System.out.println("Volume of the sun is " + sunVol + " cubic miles");
    System.out.println("The sun's volume is " + ratioVol +
					" times greater than the earth's.");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -