String类(牛刀小试)

题目1:键盘录入一个字符串,统计字符串大小写字母和数字字符的个数。

import java.util.Scanner;
public class Demo1 {
    public static void main(String[] args){

        //1.从键盘动态输入一个字符串
        System.out.println("请输入一个字符串(包含大小字母及数字字符):");
        Scanner input = new Scanner(System.in);
        String strInput = input.nextLine();

        //2.统计大小写字母及数字字符的个数
        int countBig = 0;
        int countSmall = 0;
        int countNum = 0;
        for(int i = 0; i < strInput.length(); i ++){
            if(strInput.charAt(i) >= 'a' && strInput.charAt(i) <= 'z'){
                countSmall ++;
            }else if(strInput.charAt(i) >= 'A' && strInput.charAt(i) <= 'Z'){
                countBig ++;
            }else if(strInput.charAt(i) >= '0' && strInput.charAt(i) <= '9'){
                countNum ++;
            }
        }

        //3.输出结果
            System.out.println("这个字符串中大写字母的个数有" + countBig + "个;");
            System.out.println("这个字符串中大写字母的个数有" + countSmall + "个;");
            System.out.println("这个字符串中大写字母的个数有" + countNum + "个。");

    }
}

题目2:定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。例如:Hello1Hello2Hello3

public class Demo {
    public static void main(String[] args){
        
        //1.定义一个整型数组
        int[] array = new int[]{1,2,3};

        //2.调用方法将数组转换为字符串
        String strRestructuring = arrayToString(array);

        //4.打印输出结果
        System.out.println("这个是拼接的字符串:" + strRestructuring);

    }
    //3.定义一个将整型数组转换为字符串数组的方法
    public static String arrayToString(int[] array){
        //定义一个字符串对象
        String strRestructuring = new String();
        //遍历数组,将每一个数转换成相对应的字符
        for(int i = 0; i < array.length; i ++){
            //调用String拼接方法
            strRestructuring = strRestructuring.concat("Hello" + array[i]);
        }
        return strRestructuring;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值