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
*///~