JavaSE篇学习之路:(二)【小试牛刀】

2022-12-01 11:03:05 浏览数 (1)

1.华氏温度&摄氏温度

代码语言:javascript复制
import java.util.Scanner;

public class Text1 {
    public static void main(String[] args) {
        System.out.println("请输入要转化的摄氏温度:");
        Scanner s=new Scanner(System.in);
        //读入摄氏温度
        double temp=s.nextDouble();
        //计算华氏温度
        double fahrenheit=(9.0/5.0)*temp 32;
        System.out.println("华氏温度为:" fahrenheit);

    }
}

2.计算小费

代码语言:javascript复制
import java.util.Scanner;

public class Text2 {
    public static void main(String[] args) {
        System.out.print("请输入一笔费用和酬金率:");
        Scanner s=new Scanner(System.in);
        //读入费用
        double tips=s.nextDouble();
        //读入酬金率
        double rate=s.nextDouble();
        //计算酬金
        double Remuneration=(tips*rate)/100 (tips*rate);//计算酬金
        //计算总费用
        double cost=tips Remuneration;
        System.out.println("酬金为:" Remuneration "t" "总费用为:" cost);
    }
}

3.个位数和

代码语言:javascript复制
import java.util.Scanner;

public class Text3 {
    public static void main(String[] args) {
        System.out.println("请输入一个0~1000的数字:");
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        if (i > 0 && i < 1000) {
            //取百位
            int x=i/100;
            //取十位
            int y=i/100;
            //取个位
            int z=i;
            System.out.println("个位数相加和为:" x y z);
        }else{
            System.out.println("对不起,您输入的数字有误!");
        }
    }
}

4.复利值

代码语言:javascript复制
import java.util.Scanner;

public class Text4 {
    public static void main(String[] args) {
        System.out.println("请输入存入的金额以及要查询余额月份:");
        Scanner s=new Scanner(System.in);
        int dollar=s.nextInt();
        int month=s.nextInt();
        double cost=0.0;

        for(int i=1;i<=month;i  ){
            cost=(dollar cost)*(1 0.00417);
            System.out.println("第" i "个月后帐户的金额为:" cost);
        }
    }
}

5.实现二进制转化十进制

代码语言:javascript复制
import java.util.Scanner;

public class Text5 {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.print("输入一个二进制数 ");
        int binaryNumber = s.nextInt();
        int decimal = 0;
        int p = 0;
        while (true) {
            if (binaryNumber == 0) {
                break;
            } else {
                int temp = binaryNumber % 10;
                decimal  = temp * Math.pow(2, p);
                binaryNumber = binaryNumber / 10;
                p  ;
            }
        }
        System.out.println("十进制数为:" decimal);
    }
}

6.实现十进制转化二进制

代码语言:javascript复制
import java.util.Scanner;

public class Text6 {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.print("输入一个十进制数 ");
        int n = s.nextInt();
        String str = "";
        while (n != 0) {
            str = n % 2   str;
            n = n / 2;
        }
        System.out.println(str);
    }
}

7.两点距离公式

代码语言:javascript复制
import java.util.Scanner;

import static java.lang.Math.*;

public class Text7 {
    public static void main(String[] args) {
        System.out.print("请输入x1,y1的值:");
        Scanner s=new Scanner(System.in);
        double x1=s.nextDouble();
        double y1=s.nextDouble();
        System.out.print("请输入x2,y2的值:");
        double x2=s.nextDouble();
        double y2=s.nextDouble();
        //计算两点间距离
        double distance= sqrt(pow((x2-x1),2.0) pow((y2-y1),2.0) );
        System.out.println("两点间距离为" distance);
    }
}

8.三角形面积

代码语言:javascript复制
import java.util.Scanner;

import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class Text8 {
    public static void main(String[] args) {
        System.out.print("请输入x1,y1的值:");
        Scanner s=new Scanner(System.in);
        double x1=s.nextDouble();
        double y1=s.nextDouble();

        System.out.print("请输入x2,y2的值:");
        double x2=s.nextDouble();
        double y2=s.nextDouble();

        System.out.print("请输入x3,y3的值:");
        double x3=s.nextDouble();
        double y3=s.nextDouble();
        //计算三个边的距离
        double edge1= sqrt(pow((x2-x1),2.0) pow((y2-y1),2.0) );
        double edge2= sqrt(pow((x3-x2),2.0) pow((y3-y2),2.0) );
        double edge3= sqrt(pow((x1-x3),2.0) pow((y1-y3),2.0) );

        double e=(edge1 edge2 edge3)/2.0;
        //三角形面积
        double are=sqrt(e*(e-edge1)*(e-edge2)*(e-edge3));
        System.out.println("三角形面积为:" are);
    }
}

0 人点赞