zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    mysql 常用函数
    JSP 分页代码
    day15(Mysql学习)
    day14(编码实战-用户登录注册)
    Bootstrap第3天
    Bootstrap第2天
    Bootstrap 第一天
    day13(JSTL和自定义标签&MVC模型&javaweb三层框架)
    label 对齐
    Alert提示框之后跳转指定页面
  • 原文地址:https://www.cnblogs.com/bayes/p/5356826.html
Copyright © 2011-2022 走看看