集册 Java实例教程 如何使用矩形的长度和宽度计算矩形的周长。

如何使用矩形的长度和宽度计算矩形的周长。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

423
矩形的周长是2 *(长+宽)


import java.io.BufferedReader;
/* 
 来自 
*N o w  J a v a  . c o m*/

import java.io.IOException;

import java.io.InputStreamReader;

 

public class Main {

 

        public static void main(String[] args) {

               

                int width = 0;

                int length = 0;

                       

                try

                {

                        //read the length from console

                        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                       

                        System.out.println("Please enter length of a rectangle");

                        length = Integer.parseInt(br.readLine());

 /**来自 NowJava.com**/

                        //read the width from console

                        System.out.println("Please enter width of a rectangle");

                        width = Integer.parseInt(br.readLine());

                       

                       

                }

                //if invalid value was entered

                catch(NumberFormatException ne)

                {

                        System.out.println("Invalid value" + ne);

                        System.exit(0);

                }

                catch(IOException ioe)

                {

                        System.out.println(
展开阅读全文