前端及算法小汇

自定义属性

设置元素属性值:element.setAttribute("  ","   ");可设置内置属性也可设置自定义属性

<body>
	<input type="text" value="你好" data-index="0" />
</body>

<script>
	const input = document.querySelector("input");
	
	input.value= "hello";
	console.log(input.value); // "hello"

	input.setAttribute("value", "hi");
    console.log(input.getAttribute("value")); // "hi"

    input.setAttribute("data-index", "1");
    console.log(input.getAttribute("data-index")); // "1"

    input.setAttribute("data-name", "shy");
    console.log(input.getAttribute("data-name")); // "shy"
</script>

获取元素属性值:element.getAttribute(" "," ");可获取内置属性也可获取自定义属性

<body>
	<input type="text" value="你好" data-index="0" />
</body>

<script>
	const input = document.querySelector("input");
	
	console.log(input.value); // "你好"

	console.log(input.getAttribute("value")); // "你好"
	console.log(input.getAttribute("data-index")); // "0"
</script>

移除元素属性值:removeAttribute("属性名")可移除内置属性,也可移除自定义属性

<body>
	<input type="text" value="你好" data-index="0" />
</body>

<script>
	const input = document.querySelector("input");
	
	input.removeAttribute("value");
	input.removeAttribute("data-index");
</script>

向上取整函数:Math.ceil(double a); 向下取整函数:Math.floor(double a);

判断回文数

方法一:StringBuff

public class Main {

	public static void main(String[] args) {
		Scanner a=new Scanner(System.in);
		String b=a.next();
		StringBuffer c=new StringBuffer(b);
		c.reverse();
		String d=new String(c);
		if(d.equals(b)) {
			System.out.println("是回文数");
		}
		else System.out.println("不是回文数");
	}

}

方法二:

public class Main {

	public static void main(String[] args) {
		Scanner a=new Scanner(System.in);
		int b=a.nextInt();
		int x,sum=0,num;
		num=b;
		
		while (b!=0) {
				x=b%10;
				b/=10;
				sum=sum*10+x;
		}
		
		if(sum==num) {
			System.out.println("是回文数");
		}
		else System.out.println("不是回文数");
	}

}

四舍五入:

System.out.println(String.format("%.0f",7.0/2)); //4
System.out.println(String.format("%.0f",7.0/3)); //2
//若逗号后无浮点型数字要先将类型转换为float eg:(float)

进制转换:

int k=37,r=8;
System.out.println(Integer.toString(k,r));//十进制转换为r进制

System.out.println(Integer.parseInt("11",8));//其他进制转换为十进制

超大数运算:

public class Main {
    public static void main(String[] args) {
        BigInteger bigInteger1=new BigInteger("333333333333");
        BigInteger bigInteger2=new BigInteger("444444444444");
        System.out.println("两个数分别是"+bigInteger1+"和"+bigInteger2);
        //BigInteger类型数字的四则运算
        //加
        System.out.println(bigInteger1.add(bigInteger2));
        //减
        System.out.println(bigInteger1.subtract(bigInteger2));
        //乘
        System.out.println(bigInteger1.multiply(bigInteger2));
        //除
        System.out.println(bigInteger1.divide(bigInteger2));
    }
}

取值范围:

int类型:10的9次方

long类型:10的18次方

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值