疯狂java讲义第4章课后习题解答源码

t1.java

// created by dragon 
public class t1 {
    public static void main(String[] args){
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                System.out.print(i+"x"+j+"="+i*j);
                if(2<=i && j!=i) System.out.print(",");
            }

            System.out.println();
        }
    }
}

t2.java

// created by dragon 
import java.lang.Integer;
public class t2{
    public static void main(String[] args){
        int n = Integer.parseInt(args[0]);
        for(int i=1;i<=n;i++){
            for(int j=n-i;j>0;j--){
                System.out.print(" ");
            }
            for(int k=1;k<=2*i-1;k++){
                System.out.print("*");
            }
            System.out.println();

        }
    }
}

t3.java

// created by dragon
//注意 如果你是在windows命令行下,如果出现错误:编码的GBK的不可映射字符,请采用如下该编译
//javac -encoding utf-8 t3.java 
import java.lang.Double;
public class t3{
    public static void main(String[] args){
        double r= Double.parseDouble(args[0]);//r是半径
        for(double y=-r;y<=r;y+=2){//因为System.out.println看起来像隔行打印,所以步长为2
            double a=Math.asin(y/r); //根据纵行坐标y算出角度(弧度),因为y=r*sin(a),所以sin(a)=y/x;
            double x=r*Math.cos(a); //算出x坐标,不解释
            for(double j=-r;j<=r;j++){//算出来的x坐标是正的,只能是半圆,打印整个圆从-r开始
                System.out.print(Math.abs(j<0?j+x:j-x)<0.5?'*':' ');//double判断不能用==,用临近判断
            }
        System.out.println();
        }
    }
}

 

t4.java

import java.lang.Integer;
public class t4{
    public static void main(String[] args){

        // String s1 = "你是中国人";
        String s1 = args[0];
        int splitSplitLen = Integer.parseInt(args[1]);
        splitString(s1,splitSplitLen);

    }

    public static void splitString(String src, int splitLen){
        String splitStr;
        if(null ==src){
            System.out.println("No String input!");
        } else {
            // 源字符长度
            // (关键点)将String转换成byte字节数组
            byte bt[] = src.getBytes();
            // System.out.println(bt.length);
            //字符截半的话字节对于的ASC码是小于0的值 

            if(bt[splitLen]<0){
                // 对于后截半的处理
                splitLen=splitLen+1;
            }
            if(splitLen>bt.length){
                // 切的字符长度不能大于字节长度
                splitLen=bt.length;
            }
// 重新构造一个新的截断字符串
            splitStr = new String(bt,0,splitLen);
            System.out.println("splitStr = "+ splitStr);


        }
    }
}

t5.java

import java.lang.Double;
public class t5{
    public static void main(String[] args){

        // double d1 = 123456789.233;
        double d1 = Double.parseDouble(args[0]);
        // System.out.println(number[0]);
        // 将数字转换成人民大写数字
        conventNum(d1);
        }

        public static void conventNum(double d1){
            // 转为长整型
            long numZ = (long)d1;
            // System.out.println(numZ);
            // 这里只考虑小数点后两位的情况
            int numX =(int) Math.round((d1-numZ)*100);
            // System.out.println(numX);    
            String numZs = numZ+"";
            String str="";
            // System.out.println(numZs);
            // System.out.println(numZs.length());
            switch(numZs.length()){
                case 0:
                str = IntNum(0,numZs,numX);
                break;
                case 1://个
                str = IntNum(1,numZs,numX);
                break;
                case 2://十
                str = IntNum(2,numZs,numX);
                break;
                case 3://百
                str = IntNum(3,numZs,numX);
                break;
                case 4://千
                str = IntNum(4,numZs,numX);
                break;
                case 5://万
                str = IntNum(5,numZs,numX);
                break;
                case 6://十万
                str = IntNum(6,numZs,numX);
                break;
                case 7://百万
                str = IntNum(7,numZs,numX);
                break;
                case 8://千万
                str = IntNum(8,numZs,numX);
                break;
                case 9://亿
                str = IntNum(9,numZs,numX);
                break;
                default:
                System.out.println("输入数据有误,请重新输入!");
                break;
            }
            System.out.println(str);

    }

    public static String IntNum(int numZ,String numZs,int numX){
        String[] number = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        String[] danwei = new String[]{"元","拾","佰","仟","万","十万","佰万","仟万","亿"};
        String[] xiaoshu = new String[]{"角","分"};
        String ling="";
        int lenNumzs =numZs.length();
        String str="";
        for(int i=0;i

 

你可能感兴趣的:(java)