zoukankan      html  css  js  c++  java
  • 调用方法

    1.方法

        String s = "哈哈";
              p(s);

        包装类(针对于基础数据类型)

        null (引用类型)

        byte    -->Byte
                short    -->Short
                int        -->Integer
                long    -->Long

                float    -->Float
                double    -->Double

                boolean    -->Boolean

                char    -->Character

        String num = "123";
              int _num = Integer.parseInt(num); // Integer              .是调用方法
              System.out.println(_num + 20);        //字符串转换成int,结果为143

        System.out.println(Integer.MAX_VALUE);    //输出int型的最大值
            System.out.println(Long.MIN_VALUE);        //输出Long型的最小值

      System.out.println(Integer.toBinaryString(i));  //以二进制的形式打印出来
            System.out.println(Integer.toOctalString(i)); // 八进制
            System.out.println(Integer.toHexString(i)); // 十六进制

      String s2 = "10101111100";

            System.out.println(Integer.parseInt(s2, 8));    //把字符串转换成8进制的

      public static void p(Object o) {        //没有返回值的
            System.out.println(o);        //形参
        }

      例子:

      int x = 20;
            int y = 30;
            int c = add(x, y); //相加
            p(c);

        int x = 20;
              int y = 30;

        public static int add(int a, int b) {
            int result = a + b;          //只是定义了一个形参,控制着上面的x,y相加
            return result;

       字符串的截取

        String s = "abcdefg";
                System.out.println(s.length());   判断字符串的长度
                System.out.println(s.trim().length());  //先去掉两边空格,再计算长度
                System.out.println(s.charAt(2));  //取索引值上的字符.
                System.out.println(s.contains("ab")); 
    //判断ab是否在那个字符串上,如果在的话,就返回ture
                System.out.println(s.starstWith("ab"));  //找到开头的字符串,如果是的话,返回ture
                System.out.println(s.endsWith("ab"));  找到结尾的字符串,是,就返回ture
                System.out.println(s.replace("c","_"));//更换字符串中所有符合条件的字符或者字符串

                String ids = "11,14,18"
                String[] ids_ = ids.split(",");
                for ( int i = 0; i < _ids.length ; i++ ) {
                        System.out.println(ids_[i]);
                }
                
                //把字符串转换成数组.
                System.out.println(String.valueOf(123));//将任意一个参数或者对象转换成String的格式输出
                System.out.println(s.indexOf("c"));  //取c这个字符串第一次出现的索引位置
                System.out.println(s.substring(3));   //取索引值3后面的字符串

        练习

        String str = "Nothing is impossible to a willing heart";
            String str2 = "No cross, no crown.";

            1, 打印整个字符串去掉所有空格之后的长度
            2, 写代码找出字母"o","s"所在字符串str中第一次出现的索引位置, 找出字符串中最后一个"t"的索引位置, 并输出在控制台上
            3, 写代码实现将str字符串用"空格"分割成数组, 并输出索引值为4的值
            4, 写代码实现将str字符串中所有的"i"替换成"I"
            5, 编写代码从str字符串中取每个单词的首字母打印在控制台上
            6, 在不使用第三个变量的情况下互换str和str2的值

        解:

        

    //System.out.println( str.replace(" ","").length( ));
    		/*2.*/	/*System.out.println( str.indexOf("o"));
    			System.out.println( str.indexOf("s"));
    			System.out.println( str.lastIndexOf("t"));*/
    			/*3.*/
    			/*String[] s = str.split(" ");
    			System.out.println(s[4]);*/
    			/*4.*//*System.out.println( str. replace("i","I"));*/
    
    			/*5.*/ /*String[] x = str.split(" ") ;
    				for ( int i = 0; i < x.length ; i++ ){
    						System.out.println( x[i].charAt(0));
    				}*/
    				/*6.str = str + str2;
    				str2 = str.substring( 0,str.length() - str2.length() );
    				str  = str.substring( str2.length());
    				System.out.println( str);
    				System.out.println( str2);*/
    

         

       写一段代码, 可以取出任意qq邮箱地址中的qq号码

      

    String code = "2728911@qq.com";
    		System.out.println( code.substring(0,code.indexOf("@")));
    

       

      使用for和if打印一个空心正方形,效果如下图:

      

        

              int  n = 6;
    		for ( int i = 1; i < n ; i++ ){
    				for ( int j = 1; j < n ; j++ ){
    					if ( i == 1 || i == n -1){
    						System.out.print( "*");
    					
    					} else { 
    							if ( j ==1 || j == n - 1){
    								System.out.print("*" );
    							} else { 
    									System.out.print(" " );
    							}
    					
    					}
    				}
    				System.out.println( );
    		}            
    

    总结:最外边的循环是用来控制行的,里面的是用来控制列的,先给一个判断,选出第一行与最后一行 ,这时添加的是*.否则则走的是再给一个判断,选出第一列与最后一列,添加的是*,其余的都是空.注意的是不换行输出.

        使用for循环打印一个菱形,效果如下图:

        

    思路图:

    总结:最外面的for控制的是行数,我们可以把*前面看成是0,而且把其分成上下两部分,第一个for里  取的是0的位置,后面的就是*的位置.同理,下面的也是这样取出的

    int rows = 4;
    		for ( int i = 0; i < rows ; i++ ) {
    			for ( int j = 0; j < 3 - i ; j++ ) {
    				System.out.print( " ");
    			}
    			for ( int k = 0; k < 2 * i + 1 ; k++) {
    				System.out.print( "*");
    			}
    			System.out.println( );
    		}
    		for ( int i = 0; i < rows - 1 ; i++ ) {
    			for ( int j = 0 ; j < i + 1 ; j++ ) {
    				System.out.print( " ");
    			}
    			for ( int k = 0; k < 5 - 2 * i ; k++) {
    				System.out.print("*" );
    			}
    			System.out.println( );
    		}
    

      

  • 相关阅读:
    新版本ADT创建Android项目无法自动生成R文件解决办法
    关联android-support-v4源码关联不上的解决办法
    关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法
    如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
    ProgressBar+WebView实现自定义浏览器
    Android之ProgressBar读取文件进度解析
    Android开发之ListView添加多种布局效果演示
    ubuntu释放snapd旧文件
    rte_kni
    follow RISC-V
  • 原文地址:https://www.cnblogs.com/zuo72/p/7847166.html
Copyright © 2011-2022 走看看