java第一次上机作业

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);

       

 

    }

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值