Java语言——算术运算及字符串的表示

算术运算应该使用什么类

Java.math.*类中:

Math.sqrt() : 计算平方根

Math.cbrt() : 计算立方根

Math.pow(a, b) : 计算a的b次方

Math.max( , ) : 计算最大值

Math.min( , ) : 计算最小值

Math.abs() : 取绝对值

Math.ceil(): 天花板的意思,就是逢余进一

Math.floor() : 地板的意思,就是逢余舍一

Math.rint(): 四舍五入,返回double值。注意.5的时候会取偶数

Math.round(): 四舍五入,float时返回int值,double时返回long值

Math.random(): 取得一个[0, 1)范围内的随机数

package ch2;
import java.math.*;
import java.lang.Math.*;
public class KY6_4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Math.abs(-5.8));   //绝对值
        System.out.println(Math.ceil(3.2));  //返回大于或等于指定表达式的最小整数,即向上取整
        System.out.println(Math.floor(3.8));  //取不大于x的最大整数,即向下取整
        System.out.println(Math.round(3.8));   //四舍五入
        System.out.println(Math.round(3.2));    //四舍五入
        System.out.println(Math.min(3, 2));   //二者取小值
        System.out.println(Math.max(3, 2));   //二者取大值
        System.out.println(Math.max(Math.PI,4));   //取PI与4的最大值
        System.out.println(Math.log(7.0)) ;    //输出以e为底的7的对数
        System.out.println(Math.pow(7, 2));    //输出7的2次方
        System.out.println(Math.exp(0.4));    //输出e的0.4次方
        System.out.println("e is:"+Math.E);    //输出e值
        System.out.println("π is"+Math.PI);   //输出π值
        System.out.println(Math.random());   //生成随机数
	}

}

Java语言中如何表示字符串?

使用字符串有两个步骤:

1)定义并初始化字符串。

2)使用字符串对字符串进行一些处理。

//创建一个字符串对象“Hello World”

1.String s=”Hello World”;

2.Stirng s2=new String(“Hello World”);

有关字符串String的基本方法

1)获取字符串长度    s.length();

2)检查是否为空(无内容)  boolean b=s.isEmpty();

注意字符串的empty无内容和空的String s=null;的区别,前者表示有对象没有内容,后者无对象。

字符串比较

 String a ="abc";   

 String b="abc";

 boolean r=a.equals(b);     

3)将字符串转为大写

 a.toUpperCase();

4)将字符串转为小写

 a.toLowerCase();

5)忽略大小写比较

 a.equalsIgnoreCase();

String str3=”abc”;

6)获取某个位置的字符,下标从0开始

char a= str3.toCharAt(3);

如果下标大于字符串长度-1则会出现下标越界异常StringIndexOutOfBoundsException

7)字符串拼接

可以使用+多个字符或者使用字符串的concat方法

8)查询字符串的位置

str.indexOf(s);//参数为要查询的字符,结果从0开始查询出第一次遇到的字符下标,如果没有找到返回-1

str.indexOf(s.startIndex);//第二个参数起表示起始位子

9)字符串截取

str.subString(1,3);//前包括后不包括

str.subString(3);//表示从当前位置开始截取到末尾

StringBuffer/StringBuilder

StringBuffer sc=new StringBuffer(“Hello”);

1)添加内容(添加到同一段内容中)

sc.append(“abc”).append(true).append(123);

2)转为字符串

String s=sc.toString();

3)删除内容

sc.delete(sc.length()-3,sc.length());

4)插入内容

sc.insert(8,”,”);

5)翻转

sc.reverse();

package ch2;

public class KY6_5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
     String s=new String("This is an demo of the String method.");
     System.out.println("Length:"+s.length());  //返回字符串的长度
     System.out.println("SubString:"+s.substring(11, 15));  //返回以上字符串中第12-15个字符
     StringBuffer sf=new StringBuffer("Hello World");
     sf.append("Hello Java");    //追加字符串“Hello Java”到当前对象的末尾
     sf.insert(12, "And");    //在第12个字符后插入“And”
     System.out.println(sf);
     System.out.println(sf.charAt(0));    //获得指定位置的字符,即索引号为0的字符
     sf.setCharAt(0, 'h');       //重置索引号为0处的字符
     System.out.println(sf.charAt(0));
     System.out.println(sf);
     
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值