Java日常练习一(循环+Character)

7 篇文章 0 订阅
6 篇文章 0 订阅

不经常用到的循环语法:

//while
        int i = 10;
        while (i > 5){
            System.out.println("i = " + i);
            i--;
        }

        //do-while
        int j = 10;
        do {
            System.out.println("j = " + j);
            j--;
        }while (j>5);

        // for each
        int [] numbers = {10, 20, 30, 40, 50};
        for (int x: numbers) {
            System.out.println("x = " + x);
        }

        //switch case
        String res = "1";
        switch (res){
            case "1":
                System.out.println("1");break;
            case "q":
                System.out.println("q");break;
            case "M":
                System.out.println("M");break;
            default:
                System.out.println("no");
        }



    }
//while
while("判断"){
    //执行语句
}

//do-while
do{
    //执行语句
}while("判断");

//switch
switch(Key){
    case Value1: 
        //action1;
        break;
    case Value2:
        //action2
        break;
    default:
        //action3

}


//foreach
int[] ints = new int[]{}
for(int i : ints){
    //action
}


Character 方法

public static void main(String[] args){
        String s = "asd12312ZDFCasd";
        char[] chars = s.toCharArray();
        ArrayList<String> arraysStr = new ArrayList<String>();
        ArrayList<Integer> arraysInt = new ArrayList<Integer>();

        for (char c : chars){
            if (Character.isLowerCase(c)){
                arraysStr.add(String.valueOf(Character.toUpperCase(c)));
            }else if(Character.isUpperCase(c)){
                arraysStr.add(String.valueOf(c));
            }else if (Character.isDigit(c)){
                arraysInt.add(Integer.valueOf(c));
            }
        }
        System.out.println();
    }

char转String的方法:

String.valueOf(c)

这个方法运行的arraysInt结果为ASCII码,而非12312;

那么char转Integer的方法是什么呢?

思路一,先转为String,再使用String.ParsaInt方法

String ss = "9";
Integer integer = Integer.parseInt(ss);

思路二,使用CharaCter自带的方法:

Character.getNumericValue

更新之后代码如下:

public static void main(String[] args){
        String ss = "9";
        Integer integer = Integer.parseInt(ss);
        String s = "asd12312ZDFCasd";
        char[] chars = s.toCharArray();
        ArrayList<String> arraysStr = new ArrayList<String>();
        ArrayList<Integer> arraysInt = new ArrayList<Integer>();

        for (char c : chars){
            if (Character.isLowerCase(c)){
                arraysStr.add(String.valueOf(Character.toUpperCase(c)));
            }else if(Character.isUpperCase(c)){
                // char转String的方法
                arraysStr.add(String.valueOf(c));
            }else if (Character.isDigit(c)){
                arraysInt.add(Character.getNumericValue(c));
            }
        }
        System.out.println();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值