2018-11-23

2018-11-24  本文已影响0人  HughLife
习题1

源代码:


       publicstatic void main(String[] args) {

              int[]x= new int[20];

              doublesum=0;

              for(int i = 0; i < x.length; i++) {

                     x[i]=100+(int)(Math.random()*900);

                     sum+=x[i];

                     System.out.print(x[i]+" ");   

                     if((i+1)%5==0) {

                            System.out.println();

                     }     

              }

              System.out.println("该数组的平均数为:"+(sum/20));

       }

}

运行图 习题2

源代码:


       publicstatic void main(String[] args) {

              Strings1 = new String("you are a student");

              Strings2 = new String("how are you");

              if(s1.equals(s2)) {  //使用equals方法判断s1与s2是否相同

                     System.out.println("s1与s2相同。");

              }else{

                     System.out.println("s1与s2不同");

              }

              Strings3 = new String("2203021985");

              if(s3.startsWith("220302")) {//判断s3的前缀是否是“220302”

                     System.out.println("吉林省的身份证。");

              }

              Strings4= new String("你");

              Strings5= new String("我");

              if(s4.compareTo(s5)>0) {//按着字典序s4大于s5的表达式

                     System.out.println("按字典序s4大于s5.");

              }else{

                     System.out.println("按字典序s4小于s5.");

              }

              intposition= 0;

              Stringpath= "c:\\java\\jsp\\A.java";

              position=path.lastIndexOf("\\");   //获取path变量中最后出现目录分割符号的位置

              System.out.println("c:\\\\java\\\\jsp\\\\A.java中最后出现\\的位置是:"+position);

              StringfileName= path.substring(12);  //获取path中“A.java”字符串

              System.out.println("c:\\java\\jsp\\A.java含有的文件名是:"+fileName);

              Strings6 = new String ("100");

              Strings7 = new String ("123.678");

              intn1=Integer.valueOf(s6);  //将s6转换成int型数据

              doublen2 = Double.valueOf(s7); //将s7转换成double型数据

              doublem = n1+n2;

              System.out.println(m);

              Strings8 =String.valueOf(m);      //String调用valueOf(int m)方法将m转换为字符串对象

              position=s8.indexOf(".");

              Stringtemp= s8.substring(position+1);

              System.out.println("数字"+m+"有"+temp.length()+"位小数");

              Strings9 = new String ("ABCDEF");

              chara[]=s9.toCharArray();   //将s9存放到数组a中

              for(int i = a.length-1;i>=0;i--) {

                     System.out.print(""+a[i]);

              }

       }

}

运行图
上一篇 下一篇

猜你喜欢

热点阅读