4.1
importjava.util.Scanner;
publicclass fgh {
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter the length from the center to avertex:");
double r=input.nextDouble();//declare r
double s=2*r*Math.sin(Math.PI/5);
double area=(5*s*s/(4*Math.tan(Math.PI/5)));
System.out.printf("The area of thepentagon is %.2f",area); }
}
4.2
importjava.util.Scanner;
publicclass fgh {
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter point 1 (latitude andlongitude) in degrees:");
double x1=input.nextDouble();//declare x and y
double y1=input.nextDouble();
System.out.print("Enter point 2 (latitude andlongitude) in degrees:");
double x2=input.nextDouble();
double y2=input.nextDouble();
double r=6371.01;
x1=Math.toRadians(x1);
x2=Math.toRadians(x2);
y1=Math.toRadians(y1);
y2=Math.toRadians(y2);
double d=r*Math.acos((Math.sin(x1))*(Math.sin(x2))
+Math.cos(x1)*Math.cos((x2))*Math.cos((y2-y1)));
System.out.printf("The distance between the twopoints is %.2f km",d);
}
}
4.24(对三个城市进行排序)
package gsn;
import java.util.Scanner;
publicclass sfe {
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter the first city:");
String city1=input.nextLine();
System.out.print("Enter the second city:");
String city2=input.nextLine();
System.out.print("Enter the third city:");
String city3=input.nextLine();
if(city1.compareTo(city2)<0&&city1.compareTo(city3)<0&&city2.compareTo(city3)<0)
System.out.println("The three cities in alphabeticalorder are "+city1 +" "+city2 +" " +city3);
if(city1.compareTo(city2)<0&&city1.compareTo(city3)<0&&city2.compareTo(city3)>0)
System.out.println("The three cities in alphabeticalorder are "+city1 +" "+city3 +" " +city2);
if(city1.compareTo(city2)>0&&city1.compareTo(city3)<0&&city2.compareTo(city3)<0)
System.out.println("The three cities in alphabeticalorder are "+city2 +" "+city1 +" " +city3);
if(city1.compareTo(city2)>0&&city1.compareTo(city3)>0&&city2.compareTo(city3)<0)
System.out.println("The three cities in alphabeticalorder are "+city2 +" "+city3 +" " +city1);
if(city1.compareTo(city2)<0&&city1.compareTo(city3)>0&&city2.compareTo(city3)>0)
System.out.println("The three cities in alphabeticalorder are "+city3 +" "+city1 +" " +city2);
if(city1.compareTo(city2)>0&&city1.compareTo(city3)>0&&city2.compareTo(city3)>0)
System.out.println("The three cities in alphabeticalorder are "+city3 +" "+city2 +" " +city1);
}
}
4.25
packagecarnumble;
import java.util.Random;
publicclass car {
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
int []letter=newint[3];
for(inti=0;i<3;i++)
{
letter[i]=(int)(Math.random()*26)+65;//用ASCII码值表示大写字母
System.out.print((char)letter[i]);//将int型强制转换为char型
}
System.out.println((int)(Math.random()*10000)+1000);
}
}
4.26
packagedollar;
importjava.util.Scanner;
publicclass dollar1 {
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter an amount in string:");
String amount=input.nextLine();
inti=amount.indexOf(".");//找到小数点的位置
//System.out.println(i);显示小数点的位置
String pre_amount=amount.substring(0,i);//取出小数点前面的位置
String last_amount=amount.substring(i+1,amount.length());//取出小数点后面的位置
System.out.print(pre_amount+"."+last_amount);
}
}