程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入一行字符,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。
例:括号内是说明
输入
2 (repeat=2)
Reold building room 123
Programming is fun
输出
4
3
import java.util.Scanner;
public class Test40031{
public static void main(String []args ){
int ri, repeat,count, word,i;
String line;
char c;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();in.nextLine();
for(ri=1; ri<=repeat; ri++){
line=in.nextLine();
/*---------*/
System.out.println( count);
}
}
}
老师上课讲的方法没听明白只好~~求问各位大虾吖~~import java.util.Scanner;
public class Test40031{
public static void main(String []args ){
int ri, repeat,count, word,i;
String line;
char c;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();in.nextLine();
for(ri=1; ri<=repeat; ri++){
line=in.nextLine();
count=0;
for(i=0;i<line.length();i++)
{ if(line.charAt(i)==' '&&line.charAt(i+1)!=0)
{count++;
i++;
}}
System.out.println( count);
}
}
}自己这个代码最后结果和题目里的一样~但提交之后显示错误~~
哪位大虾能帮忙改一下么~~~?
谢谢谢谢啦~~~
用java写一个统计字符串中指定单词的个数
单词是s="book";直接输出str.split(s).length-1就是单词的个数。原理就是字符串前后都加空格以后,再用你要查找的单词把字符串分成数组,数组元素的个数减1,就是单词的个数了。当然,首先要用contains() 方法判断一下字符串是否已经包含要找的单词。
用java 编写一个程序,接受用户输入的一段英文文字,统计出其中的字符个...
System.out.println("单词个数为:" + dc);System.out.println("字母数为:" + (zc - 1));System.out.println("句数为:" + jc);}
java统计单词的个数
import java.util.Scanner;public class Test40031 { public static void main(String []args ){ int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner(System.in);repeat=(in.nextLine()).charAt(0)-'0'; \/\/输入repeat for(ri=1; ri<=repeat; ri++){ line=i...
java编程:输入一行英文,判断中间有多少个单词,单词间以空格隔开,连续出 ...
Java程序:public class Main {public static void main(String[] args){String text = "How do you do!";System.out.println(getWords(text));}public static int getWords(String text) {int count = 0;int len = text.length();int i, j;i = 0;while(true) {for(; i<len && tex...
*用java做!输入一行字符(英语句子),包含若干单词,相邻的两个单词用...
import java.util.Scanner;public class LogInSys { public static void main(String[] args) { \/\/ TODO Auto-generated method stub int LogOut = 1;Scanner input = new Scanner(System.in);System.out.println("欢迎使用XX登录系统");do{ System.out.print("请输入用户名:");String ...
怎么用JAVA输出一句话单词的个数和大写字母的个数?
";\/\/ 单词数int count = 0;\/\/ 大写字母个数int upper = 0;StringBuffer words = new StringBuffer();StringBuffer chars = new StringBuffer();String s[] = str.split(" |,|\\\\?|\\\\.");\/\/ split()里面是正则表达式for (int i = 0; i < s.length; i++) {String word = s[...
输入一行字符,统计其中的单词的个数,单词之间以单个空格作为分隔符_百 ...
LZ的代码统计的不是单词,应该是统计的字母。如果str="this is word!" 怕是就统计的不是单词了 linux0510 | 发布于2013-05-15 举报| 评论 0 0 其他1条回答 为您推荐: c语言统计单词个数 输入字符串 统计文本中单词的个数 输入一行字符分别统计 python统计文件单词 字符串数组 统计一行字符的...
java:给定一串字符串(英文段落),用户输入某个单词,求该单词出现的总次数...
+ count + " 次.");}}public static void main ( String[] args ){String input = "Look buddy, U got work hard and put yourself to the java, once U learned the heart of java, I can guarantee that U win.";String word = "java";readWord (input, word, 0, 0);}} ...
Java编程!键盘输入一段英文语句,将这段话写入文件file.txt中,然后统...
import java.util.Scanner;public class Test2 { public static void main(String[] args) throws Exception{ Scanner san = new Scanner(System.in);System.out.println("Input the text:");String strText = san.nextLine();String[] strArr = strText.split(" ");File file = new File("...
用java怎么写出算一段英文单词总数和每个单词出现的次数?
import java.util.*;import java.util.regex.*;public class Yugi{ public static void main(String[] args){ String words = "Look buddy, U got work hard and put yourself in your java, Once you learned the heart of the java, I can guarantee that you win."; String reg...