【Java】1.顺时针打印矩阵,2.统计1~n中数字出现1的个数,3.字符串转换整型 例如 123转换成123,sjdh--- 2345dj5转换成23455

1.顺时针打印矩阵

package com.tulun.src;

import java.util.Scanner;

public class Arr4 {
 public  static  void main(String[] args){
        /**
         * 顺时针打印矩阵:
         */
        int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        int circle = arr[0].length/2;
        int count = 0;
        while(count<circle){
            //上
            for(int colum = count;colum < arr[0].length-count;colum++){
                System.out.print(arr[count][colum]+" ");
            }
            //右
            for(int row = count+1;row < arr.length-count;row++){
                System.out.print(arr[row][arr[0].length-1-count]+" ");
            }
            //下
            for(int colum = arr[0].length-1-1-count;colum > count;colum--){//三行四列
                System.out.print(arr[arr.length-1-count][colum]+" ");
            }
            //左
            for(int row = arr.length-1-1-count+1;row >= count+1.;row--){//三行四列
                System.out.print(arr[row][count]+" ");
            }
            count++;
        }
    }
}

2.统计1~n中数字出现1的个数

package com.tulun.src;

import java.util.Scanner;

public class Arr4 {
/**
     * 输入整数n,求1~n整数中数字1出现的次数
     */
    public static int time(int n){
        int count = 0;
        for(int i=1;i<=n;i++){
            int src = i;
            while(src!=0) {
                if (src % 10 == 1) {
                    count++;
                }
                src/=10;
            }
        }
        return count;
    }
    public  static  void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = time(n);
        System.out.println(count);
    }
}

3.字符串转换整型 例如 123转换成123,sjdh— 2345dj5转换成23455

package com.tulun.src;

import java.util.Scanner;

public class Arr4 {
    /**
     * 字符串转换整型 例如 123转换成123,sjdh--- 2345dj5转换成23455
     */
    public static int getCount(char[] ch){
        int count = 0;
        for(int i=0;i< ch.length;i++){
            if(ch[i] >= '0' && ch[i]<='9'){
                count++;
            }
        }
        return count;
    }
    public static int trans(String str){
        char[] ch = str.toCharArray();//'-' '1''2''3'
        int count = getCount(ch);
        int size = 0;
        int[] arr = new int[count];//1  2  3
        int flag = 1;//统计正负号
        for(int i=0;i<ch.length;i++){
            if(ch[i]>='0'&& ch[i]<='9') {
                arr[size++] = ch[i] - '0';
            }
            if(ch[i] == '-'){
                flag *=-1;
            }
        }//1 2 3 4
        int result = 0;
        int exp = count;
        for(int i=0;i<count;i++){
            result+=arr[i]*Math.pow(10,--exp);
        }
        result*=flag;
        return result;
    }
    public  static  void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();//"123 456"
        int result =trans(str);
        System.out.println(result);
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值