【Java-简单练习题】

1."AABBBCCC">>"A2B3C3"

java 复制代码
public class Test6 {
	public static void main(String[] args) {
		String ns = "AABBBCCCC";
		String ret=compress(ns);
		System.out.println(ret);
	}
	
public static String compress(String str) {
	StringBuilder ret = new StringBuilder();
	int count = 1;
	// 遍历至倒数第二个
	for (int i = 0; i < str.length()-1; i++) {
		if (str.charAt(i) == str.charAt(i + 1)) {
			count++;
		} else {
			ret.append(str.charAt(i)+""+count);
			count=1;
		}
	}
	//处理最后一个字符
	ret.append(str.charAt(str.length()-1)+""+count);
	return ret.toString();
}
}
  1. 图书ISBN验证码
java 复制代码
package ti;
import java.util.Scanner;
public class Test02 {
	 public static void main(String[] args) {
	        Scanner scan = new Scanner(System.in);
	        //在此输入您的代码...
	        //输入ISBN
	        String isbn=scan.next();
	        //去除分隔符
	        String isbn1=isbn.replace("-","");
	        //累加
	        int sum=0;
	        for(int i=0,k=1;i<isbn1.length()-1;i++,k++){
	            int na=Integer.parseInt(String.valueOf(isbn1.charAt(i)));
	            sum=sum+na*k;
	        }
	        //计算校验码
	        String code=String.valueOf(sum%11==10?"X":sum%11);
	        //判断
	        String isbncode=String.valueOf(isbn.charAt(isbn.length()-1));
	        if(code.equals(isbncode)){
	          System.out.println("Right");
	        }else{
	        	 System.out.println(isbn.substring(0,isbn.length()-1)+code);
	        }
	        scan.close();
	    }}
  1. 统计字母数字中文符号的个数
java 复制代码
public static void main(String[] args) {
		String str = "OMG,你们的中英混搭真是各有千秋,666但Someone丝毫掩盖不了你们那硬朗的英语底子!For eg.papi酱真的very有才华啊";
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("letters", 0);
		//map.put("numbers", 0);
		map.put("chinese", 0);
		map.put("flags", 0);
		// 遍历字符串
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
				int oldValue = map.get("letters");
				int newValue = oldValue + 1;
				map.put("letters", newValue);
			}if(c >= '0' && c <= '9') {
				map.put("numbers", map.getOrDefault("numbers",0)+1);
			}if(c >= 0x4e00 && c <= 0x29fa5) {
				map.put("chinese", map.get("chinese")+1);
			}else {
				map.put("flags", map.get("flags")+1);
			}
		}
		System.out.println(map);
	}
  1. 幸运数字
java 复制代码
package learn01;

import java.math.BigInteger;

public class Test02 {
	public static void main(String[] args) {
		int i = 1, counter = 0;
		while (true) {
			String bin = Integer.toBinaryString(i);
			String oct = Integer.toOctalString(i);
			String dec = String.valueOf(i);
			String hex = Integer.toHexString(i);

			int binsum = sum(bin, 2);
			int octsum = sum(oct, 8);
			int decsum = sum(dec, 10);
			int hexsum = sum(hex, 16);

			if (i % binsum == 0 && i % octsum == 0 && i % decsum == 0 && i % hexsum == 0) {
				System.out.println(i);
				counter++;
			}
			if(counter>=2023) {
				break;
			}
			i++;
		}
	}
	
	private static int sum(String number,int radix) {
		BigInteger ret=new BigInteger("0",radix);
		
		for(int i=0;i<number.length();i++) {
			BigInteger bn=new BigInteger(number.substring(i,i+1),radix);
			ret=ret.add(bn);
		}
		return ret.intValue();
	}
}
相关推荐
布朗克1681 小时前
Java 10 新特性及具体应用
java·开发语言·新特性·java10
ZZHow10244 小时前
JavaWeb开发_Day05
java·笔记·web
CHEN5_024 小时前
【Java虚拟机】垃圾回收机制
java·开发语言·jvm
Warren984 小时前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
HalvmånEver4 小时前
在 C++ :x86(32 位)和 x64(64 位)的不同
开发语言·c++·学习
amy_jork6 小时前
npm删除包
开发语言·javascript·ecmascript
浪成电火花7 小时前
(deepseek!)deepspeed中C++关联部分
开发语言·c++
茉莉玫瑰花茶7 小时前
Qt 常用控件 - 9
开发语言·qt
独行soc7 小时前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试
S01d13r8 小时前
gunicorn + flask 处理高并发请求
python·flask·gunicorn