• Java入门:基础算法之计算园的面积


    本部分内容介绍如何使用Java计算圆的周长和面积。分两种方法来实现:

    1)圆的半径由用户输入

    2)圆的半径由程序指定

    代码1:

    /**
     * @作者: 理工云课堂
     * @描述: 用户输入圆的半径,程序结算周长和面积
     */
    import java.util.Scanner;
    class CircleDemo
    {
       static Scanner sc = new Scanner(System.in);
       public static void main(String args[])
       {
          System.out.print("Enter the radius: ");
          //将半径保存在double型变量中
          double radius = sc.nextDouble();
          //公式:面积 = PI*radius*radius
          double area = Math.PI * (radius * radius);
          System.out.println("The area of circle is: " + area);
          //公式:周长= 2*PI*radius
          double circumference= Math.PI * 2*radius;
          System.out.println( "The circumference of the circle is:"+circumference) ;
       }
    }

    输出结果:

    Enter the radius: 1
    The area of circle is: 3.141592653589793
    The circumference of the circle is:6.283185307179586

    代码2:

    /**
     * @作者: 理工云课堂
     * @模拟奥数: 程序计算圆的周长和面积,圆的半径在程序中指定,不需要用户输入.
     */
    class CircleDemo2
    {
       public static void main(String args[])
       {
          int radius = 3;
          double area = Math.PI * (radius * radius);
          System.out.println("The area of circle is: " + area);
          double circumference= Math.PI * 2*radius;
          System.out.println( "The circumference of the circle is:"+circumference) ;
       }
    }

    输出结果:

    The area of circle is: 28.274333882308138
    The circumference of the circle is:18.84955592153876

    问题:请修改代码,将结果保留两位小数输出。

  • 相关阅读:
    C# Enum转换
    Split
    WCF访问安全
    [转] 检索 COM 类工厂中 CLSID 为 {000209FF00000000C000000000000046} 的组件时失败
    ICSharpCode.SharpZipLib.dll压缩的zip包,7zip解压时出错
    js控制ctrl+p
    跨域访问WCF问题
    sql:过滤字段中是否包含数字
    序列化/反序化
    [转]RegistryKey 操作注册表
  • 原文地址:https://www.cnblogs.com/bayes/p/5356826.html
Copyright © 2020-2023  润新知