Welcome to JAVA!(第五课课后练习)

5.4*Write the following method to display an integer in reverse order:

public static void reverse (int number)

For example,reverse(3456) display 6543.Write a test program that that prompts the user to enter an integer and displays its reversal.

代码如下:

import java.util.Scanner;
public class Main
{
	public static void main(String[] args){
		 Scanner input=new Scanner(System.in); 
		 System.out.print("Please input a number:");
		 int x=input.nextInt();
		 reverse(x);
		
		
	}
	
	public static void reverse(int number)
	{
		int num=0;
		while(number>0)
		{
			num=num*10+number%10;
			number/=10;
		}
		 System.out.println("The number of the display to reverse:" + num);
	}
}
运行结果:

/*output:
Please input a number:3456
The number of the display to reverse:6543
*///~

5.19* Create a class named MyTriangle that contains the following two methods:

public static boolean isVaild(double side1, double side2,double side3)

public static double area(double side1,double side2,double side3)

Write a test program that reads three sides for a triangle and computes the area if the input is valid.Otherwise,it displays that the input is invaild.

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main
{
	public static void main(String[] args){
		 Scanner input=new Scanner(System.in); 
		 System.out.print("Please enter a triangle of three sides long:"); 
		 double num1=input.nextDouble();
		 double num2=input.nextDouble();
		 double num3=input.nextDouble();
		 if(isValid(num1,num2,num3))  
	        {  
			 	DecimalFormat df = new DecimalFormat("0.000"); 
	            double S = area(num1,num2,num3);  
	            System.out.println("The area of a triangle is:" + df.format(S));  
	        }  
	        else  
	            System.out.println("You enter the three sides can't form a triangle!");  
	}
	
	public static boolean isValid(double side1, double side2,double side3)
	{
		if((side1+side2>side3)&&(side1+side3>side2)&&(side3+side2>side1))  
            return true;  
        else  
            return false; 
	}
	
	public static double area(double side1,double side2,double side3)
	{
		double p=(side1+side2+side3)/2;  
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));  
    }  
}
	

运行结果:

/*output:
Please enter a triangle of three sides long:1.5 4.6 5
The area of a triangle is:3.427
*///~

5.36 A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree(i.e.,the polygon is both equilateral and equiangular).The formula for computing the area of a regular polygon is

Area=(n*s^2)/(4*tan(π/n)

Write a method that returns the area of a regular polygon using the following header:

public static double area(int n,double side)

Write a main methid that prompts the user to enter the number of sides and the side of a regular polygon and display its area.

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main
{
	public static void main(String[] args){
		
		 Scanner input=new Scanner(System.in); 
		 System.out.print("Please enter a number of regular polygon:"); 
		 int n=input.nextInt();
		 System.out.print("Please enter the length of a regular polygon:"); 
		 double side=input.nextDouble();
		 DecimalFormat df = new DecimalFormat("0.000");
		 System.out.println("The area of the regular polygon is:" + df.format(area(n,side)));
		 
	}
	
	
	public static double area(int n,double side)
	{
		return (n*side*side)/(4*Math.tan(Math.PI/n));
	}
}
	

运行结果:

/*output:
Please enter a number of regular polygon:10
Please enter the length of a regular polygon:5
The area of the regular polygon is:192.355
*///~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值