黑马程序员技术交流社区

标题: 面试题 [打印本页]

作者: lf900827    时间: 2015-8-28 20:13
标题: 面试题
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
作者: jumtop    时间: 2015-8-28 21:14
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s.length());
作者: 方育伟    时间: 2015-8-28 21:39
.length();
作者: jeska    时间: 2015-8-28 22:47
啊?这是面试题?
作者: MilesMatheson    时间: 2015-8-28 23:05
package Temp;

import java.util.Scanner;

public class 字符串长度 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入你的字符串:");
                String str = sc.nextLine();
                //方式一:所谓的方法求出字符串长度底层代码这么些:
            /**
             * Returns the length of this string.
             * The length is equal to the number of <a href="Character.html#unicode">Unicode
             * code units</a> in the string.
             *
             * @return  the length of the sequence of characters represented by this
             *          object.
             */
/*            public int length() {
                return value.length;
            }
            
            
//                 The value is used for character storage.
//                                private final char value[];
            
            */
            
            
               
                System.out.println(numOfString(str));
                //方式二:直接调用String类的方法求出字符串
                System.out.println(str.length());
                //所以我认为定义一个函数求出字符串的长度是一件蛋疼事情;要说算算里面有多少个a,多少个b还好;
               
               

        }
        public static int numOfString(String str)
        {
                //定义临时数组用于存储字符串转换后的字符数组;
                char[] strArry = new char[1024];
                //转成字符数组并存入
                strArry = str.toCharArray();
                //遍历字符数组
                return strArry.length;
        }

}


作者: cemabenteng    时间: 2015-8-28 23:11
这不会是面试题,太简单了
作者: a348639336    时间: 2015-8-28 23:15
这是就业班面试题?!有这么简单吗?
Scanner获取.length()就好了吧?
如果要复杂点就存入字符数组,然后获取数组长度就好了
作者: superhs    时间: 2015-8-28 23:33
这么简单的??




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2