如何使用java将Java is an object oriented programming language输出,并且计算出单词的平均长度
4个回答
展开全部
写个最短的吧
public class Temp {
public static void main(String[] args){
String string = "Java is an object oriented programming language";
String[] words = string.split(" ");
System.out.println("语句:" + string);
System.out.println("单词平均长度:" + (string.replace(" ","").length()/words.length));
}
}
public class Temp {
public static void main(String[] args){
String string = "Java is an object oriented programming language";
String[] words = string.split(" ");
System.out.println("语句:" + string);
System.out.println("单词平均长度:" + (string.replace(" ","").length()/words.length));
}
}
展开全部
import java.util.regex.Pattern;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String msg = "Java is an object oriented programming language";
String regex = " ";//正则表达式,一个空格,用以分隔字符串
String s ="";
String[] str = null;//字符数组,用以存放分隔后的各个单词
Pattern p = Pattern.compile(regex);
str = p.split(msg);
int num = str.length;
System.out.println("一共有" + num + "个单词。");
int length = msg.trim().length();//将字符串msg去掉空格后的长度
int len = length / num;
System.out.println("单词的平均长度为:" + len);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String msg = "Java is an object oriented programming language";
String regex = " ";//正则表达式,一个空格,用以分隔字符串
String s ="";
String[] str = null;//字符数组,用以存放分隔后的各个单词
Pattern p = Pattern.compile(regex);
str = p.split(msg);
int num = str.length;
System.out.println("一共有" + num + "个单词。");
int length = msg.trim().length();//将字符串msg去掉空格后的长度
int len = length / num;
System.out.println("单词的平均长度为:" + len);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String string = "Java is an object oriented programming language";
String[] tokens = string.split(" ");
int cnt = 0;
for(int i = 0; i < tokens.length; i++) {
cnt += tokens[i].length();
}
System.out.println("inputed String : " + string);
System.out.println("average length : " + (cnt/tokens.length));
String[] tokens = string.split(" ");
int cnt = 0;
for(int i = 0; i < tokens.length; i++) {
cnt += tokens[i].length();
}
System.out.println("inputed String : " + string);
System.out.println("average length : " + (cnt/tokens.length));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先以空格分段开长字符串,然后再得相应的长度,求平均值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询