5.数学函数和字符串

数学函数

Math.abs(x)     //绝对值函数abs:5
Math.max(7,99)     //max返回两个数中最大值:99
Math.min(7,99)     //max返回两个数中最小值:7
Math.pow(2,3)     //pow第一个数的第二次幂2的3次方:8.0
Math.random()     //返回0.0-1.0之间随机数
Math.ceil(3.5)     //向后四舍五入:4.0
Math.floor(3.5)     //向前四舍五入:3.0
Math.round(3.3)     //四舍五入后的整数值:3
Math.sqrt(25)     //平方根,开根号:5.0

数学函数应用

     数学函数和Math类
    public static void main(String[] args) {
        int x=-5;
        System.out.println(Math.abs(x));     //绝对值函数abs:5
        System.out.println(Math.max(7,99));     //max返回两个数中最大值:99
        System.out.println(Math.min(7,99));     //max返回两个数中最小值:7
        System.out.println(Math.pow(2,3));     //pow第一个数的第二次幂2的3次方:8.0
        System.out.println(Math.random());     //返回0.0-1.0之间随机数
        System.out.println(Math.ceil(3.5));     //向后四舍五入:4.0
        System.out.println(Math.floor(3.5));     //向前四舍五入:3.0
        System.out.println(Math.round(3.3));     //四舍五入后的整数值:3
        System.out.println(Math.sqrt(25));     //平方根,开根号:5.0
    }

    输入三角形三条边长a,b,c,求三角形面积
    public static void main(String[] args) {
        double a,b,c,s;
        Scanner reader = new Scanner(System.in);
        a = reader.nextInt();
        b = reader.nextInt();
        c = reader.nextInt();
        if (a+b>c&&a+c>b&&b+c>a){
            s = (a+b+c)/2;
            s = s*(s-a)*(s-b)*(s-c);
            s = Math.sqrt(s);
            System.out.println(String.format("构成三角形,面积是%.1f", s));
        }
        else
            System.out.println("不能构成三角形");
    }

    随机产生30个60-100分的成绩
    public static void main(String[] args) {
        double[] x;
        x = new double[30];
        for (int i=0;i<30;i++){
            x[i]=Math.random()*41+60;
        }
        for (int i=0;i<30;i++){
            System.out.print(String.format("%.2f ", x[i]));
        }
    }

字符串

//字符串的新建
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");

//字符串的输入方式
String a;
Scanner reader = new Scanner(System.in);
a = reader.next();     //字符串不包含空格
a = reader.nextline();     //字符串包含空格

s1与s2指向相同的内存地址,s3与s4指向不同的内存地址,建议使用:s1 与 s2。

字符串的比较

a==b;     //地址是否相等:false,如果使用new创建,则地址不相同
a.equals(b);     //数值是否相等:false
a.equalsIgnoreCase(b);     //忽略大小写数值是否相等:true
a.compareTo(b);     //按顺序比较两个字符串
a.compareToIgnoreCase(b);     //忽略大小写按顺序比较两个字符串

字符串的比较应用

    字符串比较
    public static void main(String[] args) {
        String a = "abcdefg";
        String b = new String("AbcdefG");
        System.out.println(a==b);     //地址是否相等:false
        System.out.println(a.equals(b));     //数值是否相等:false
        System.out.println(a.equalsIgnoreCase(b));     //忽略大小写数值是否相等:true
        System.out.println(a.compareTo(b));     //按顺序比较两个字符串
        System.out.println(a.compareToIgnoreCase(b));     //忽略大小写按顺序比较两个字符串
    }    

    输入用户名密码,判断该用户名密码是否相同
    public static void main(String[] args) {
        String a,b;
        Scanner reader =new Scanner(System.in);
        a = reader.next();
        b = reader.next();
        if (a.equals("admin")&&b.equals("admin"))
            System.out.println("seccess");
        else
            System.out.println("faild");
    }

字符串常用方法

length();     //字符串长度
+;     //字符串拼接
concat(stirng);     //字符串拼接
startsWith(stirng);     //字符串是否以指定字符串开头
endsWith(stirng);     //字符串是否以指定字符串结尾
substring(int start,int end);     //取字符串指定位置子串
charAt(int index);     //取指定位置字符
toUpperCase(Locale.ROOT);     //字符串转换为大写
toLowerCase(Locale.ROOT);     //字符串转换为小写
replace(old,new);     //字符串替换:Abcdef
trim();     //去除左右两边空格
indexOf(stirng);     //查找指定字符位置
lastIndexOf(stirng);     //从后查找指定字符位置
split(string);     //以指定字符分割字符串
String.format("%s ", a);     //字符串格式化
toCharArray();     //转换为字符数组

字符串常用方法应用

字符串常用方法
    public static void main(String[] args) {
        String a = "abcdef";
        String b[];
        System.out.println(a.length());     //字符串长度:6
        System.out.println(a+"ghijk");     //字符串拼接:abcdefghijk
        System.out.println(a.concat("ghijk"));     //字符串拼接:abcdefghijk
        System.out.println(a.startsWith("abc"));     //字符串是否以指定字符串开头:true
        System.out.println(a.endsWith("abc"));     //字符串是否以指定字符串结尾:false
        System.out.println(a.substring(2,5));     //取字符串指定位置子串:cde
        System.out.println(a.charAt(1));     //取指定位置字符:b
        System.out.println(a.toUpperCase(Locale.ROOT));     //字符串转换为大写:ABCDEF
        System.out.println(a.toLowerCase(Locale.ROOT));     //字符串转换为小写:abcdef
        System.out.println(a.replace("a","A"));     //字符串替换:Abcdef
        System.out.println(a.trim());     //去除左右两边空格:abcdef
        System.out.println(a.indexOf("ef"));     //查找指定字符位置:4
        System.out.println(a.lastIndexOf("f"));     //从后查找指定字符位置:5
        b = a.split("c");     //以指定字符分割字符串
        System.out.println(b[0]+","+b[1]);
        System.out.println(String.format("%s ", a));     //字符串格式化:abcdef
        char[] c;
        c = a.toCharArray();     //转换为字符数组:a b c d e
        for (int i=0;i<c.length-1;i++)
            System.out.print(c[i]+" ");
    }

    提取字符串中的单词并排序
    public static void main(String[] args) {
        String a,b[],c;
        Scanner reader=new Scanner(System.in);
        a = reader.nextLine();
        b = a.split(" ");
        for (int i =0;i<b.length;i++){
            for (int j=i+1;j<b.length-1;j++){
                if (b[i].compareTo(b[j])>0){
                    c = b[j];
                    b[j]=b[i];
                    b[i]=c;
                }
          }
        }
        for (int i =0;i<b.length;i++){
            System.out.print(b[i]+" ");
        }
    }

StringBuffer类

//必须使用new StringBuffer来创建
StringBuffer a = new StringBuffer(string); 

StringBuffer类的使用

length();     //输出a的长度
charAt(index);     //输出指定位置的字符
substring(start,end);     //输出指定范围的字符串,前包后不包

append(string);     //追加字符串
insert(index,string);     //插入字符串
replace(start,end,string);     //替换字
delete(start,end);     //删除字符串

StringBuffer类的使用
    public static void main(String[] args) {
        StringBuffer a=new StringBuffer("abcdefg");
        System.out.println(a.length());     //输出a的长度
        System.out.println(a.charAt(0));     //输出指定位置的字符
        System.out.println(a.substring(2,5));     //输出指定范围的字符串,前包后不包
        a.append("higk");     //追加字符串
        System.out.println(a);
        a.insert(1,"higk");     //插入字符串
        System.out.println(a);
        a.replace(0,3,"jqk");     //替换字符串
        System.out.println(a);
        a.delete(0,3);     //删除字符串
        System.out.println(a);
    }

StringBuffer类与String的区别

Sting的常用方法是创建一个新的字符串

StringBuffer则是直接改变原有字符串

作业

根据公式,求pi的值
    public static void main(String[] args) {
        int n;
        Scanner reader = new Scanner(System.in);
        n = reader.nextInt();
        double sum=0,p=0;
        for (int i=0;i<n;i++){
            sum=sum+(1/Math.pow(i+1,2));
        }
        p = Math.sqrt(6*sum);
        System.out.println(p);
    }

    输出一个字符在字符串中出现的次数
    public static void main(String[] args) {
        int count=0;
        char a,c[];
        String b = "huahuashijie";
        Scanner reader = new Scanner(System.in);
        a = reader.next().charAt(0);
        c = b.toCharArray();
        for (int i =0;i<c.length-1;i++){
            if (a==c[i])
                count++;
        }
        System.out.println(String.format("字符%c 出现%d次", a,count));
    }

    字符串替换程序
    public static void main(String[] args) {
        String news,olds,c="huashijie";
        Scanner reader = new Scanner(System.in);
        news = reader.next();
        olds = reader.next();
        if (c.indexOf(olds)!=-1){
            c = c.replace(olds,news);
            System.out.println("替换完毕:"+c);
        }else
            System.out.println("字符串不存在");
    }

    统计引文句子中每个单词出现的次数
    public static void main(String[] args) {
        String content="this is a good a beautiful a girl",a[];
        int count=0;
        a = content.split(" ");
        for (int i=0;i<a.length-1;i++){
            for (int j=0;j<a.length-1;j++){
                if (a[i].equals(a[j])){
                    count++;
                }
            }
            System.out.println(a[i]+"出现了"+count+"次");
            count=0;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值