Write a JAVA program to input the width and height of the rectangle from the keyboard, and output the circumference and area of the rectangle. (Try two input and output methods separately, the output result is accurate to two decimal places)

code show as below: 

The first:

import java.util.Scanner;
public class Area { 

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入高度:");
        double a=sc.nextDouble();
        System.out.println("请输入宽度:");
        double b=sc.nextDouble();
        double c=a*b;
        double d=2*(a+b);
        System.out.printf("面积为:%.2f\n",c);
        System.out.printf("周长为:%.2f\n",d);    
    }
}

The second kind:

import java.io.BufferedReader;    
import java.io.IOException;    
import java.io.InputStreamReader;    
import java.util.Scanner;    
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Area {

	public static void main(String[] args) {
	
		InputStreamReader is = new InputStreamReader(System.in); 
		BufferedReader br = new BufferedReader(is); 
		try{ 
		System.out.println("请输入高度:");
		String changdu = br.readLine();  
		System.out.println("请输入宽度:");
		String kuandu = br.readLine();  
		int a = Integer.parseInt(changdu); 
		int b = Integer.parseInt(kuandu);  
		double c=a*b;
		double d=2*(a+b);
DecimalFormat df = new DecimalFormat("#.00");
        System.out.println("面积为:"+df.format(c));
        System.out.println("周长为:"+df.format(d));
		
		    }   
		catch(IOException e){   
		e.printStackTrace();   
		    } 
		
	}

}

 

Published 1 original article · liked 0 · visits 6

Guess you like

Origin blog.csdn.net/weixin_44286093/article/details/105605157
Recommended