用java 编写一个程序,接受用户输入的一段英文文字,统计出其中的字符个数、单词个数和句子的个数。

设句子以“。”“!”“?”结束,单词之间利用空格分隔

public static void main(String[] args)

{

System.out.println("请输入英语片段,以';'结束:");

Scanner scanner = new Scanner(System.in);

String str = "";

int dc = 0;

int zc = 0;

int jc = 0;

while(scanner.hasNext())

{

str = scanner.next();

zc += str.length();

dc++;

System.out.println(str);

if (str.contains("."))

{

jc += 1;

}

if (str.contains(";"))

{

break;

}

}

// scanner.

System.out.println("单词个数为:" + dc);

System.out.println("字母数为:" + (zc - 1));

System.out.println("句数为:" + jc);

}

扩展资料:

import java.io.*;

public class test

{ public static void main(String[] args) throws IOException

{ System.out.print("请输入一串字母:");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

int char1=0,letter=0,sentence=0;

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

{

char ch = str.charAt(i);

if(ch!=' ')

{ char1++;

}

if(ch==' '||ch=='/n')

{ //有空格,加一单词

if(i!=0&&str.charAt(i-1)!=' ')

letter++;

}

if(ch=='?'||ch=='!'||ch=='.')

{           sentence++; 

}

}

System.out.println("字符数:"+char1);

System.out.println("单词个数:"+letter);

System.out.println("句子数:"+sentence);

}

}

import java.io.*;

public class test

{ public static void main(String[] args) throws IOException

{ System.out.print("请输入一串字母:");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

int char1=0,letter=0,sentence=0;

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

{

char ch = str.charAt(i);

if(ch!=' ')

{ char1++;

}

if(ch==' '||ch=='/n')

{ //有空格,加一单词

if(i!=0&&str.charAt(i-1)!=' ')

letter++;

}

if(ch=='?'||ch=='!'||ch=='.')

{           sentence++; 

}

System.out.println("字符数:"+char1);

System.out.println("单词个数:"+letter);

System.out.println("句子数:"+sentence);

}

}

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-03
public static void main(String []args)
{
Scanner s1=new Scanner(System.in);
System.out.println("请输入一段英文文字:");
String words=s1.nextLine();
CheckWords(words);
}
public static void CheckWords(String words)
{
int juzi=0,danci=0;
for(int i=0;i<words.length();i++)
{
if(words.charAt(i)=='.' || words.charAt(i)=='!' || words.charAt(i)=='?')
{
juzi++;
}
if(words.charAt(i)==' ')
{
danci++;
}
}
System.out.println("句子"+juzi+"个,单词"+danci+"个.");
}追问

CheckWords(words);
显示错误,怎么回事?

第2个回答  推荐于2017-11-24
public static void main(String[] args)
{
System.out.println("请输入英语片段,以';'结束:");
Scanner scanner = new Scanner(System.in);
String str = "";
int dc = 0;
int zc = 0;
int jc = 0;
while(scanner.hasNext())
{
str = scanner.next();
zc += str.length();
dc++;
System.out.println(str);
if (str.contains("."))
{
jc += 1;
}
if (str.contains(";"))
{
break;
}
}
// scanner.
System.out.println("单词个数为:" + dc);
System.out.println("字母数为:" + (zc - 1));
System.out.println("句数为:" + jc);
}追问

大师, while(scanner.hasNext()) 怎么理解,没学过,求解释

追答

读取屏幕所输入的单词,以空格为组!如how are you? scanner.hasNext()) 就是3次

本回答被提问者采纳
第3个回答  2011-11-03
package com.sh.lw.regxp;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegXp {

/**
* 计算单词总数
* @param s
*/
public int countWord(String s) {
s = s.trim();
Pattern pattern = null;
String str = "\\s";
pattern = Pattern.compile(str);
Matcher m = pattern.matcher(s);
int i = 0;
while (m.find()) {
i++;
}
return i + 1;
}

/**
* 计算句子总数
* @param s
* @return
*/
public int countStatement(String s) {
s = s.trim();
Pattern pattern = null;
String str = "[.!?]{1}";
pattern = Pattern.compile(str);
Matcher m = pattern.matcher(s);
int i = 0;
while (m.find()) {
i++;
}
return i;
}

/**
* 计算字符总数
* @param s
* @return
*/
public int countChar(String s) {
s = s.trim();
Pattern pattern = null;
String str = "\\S";
pattern = Pattern.compile(str);
Matcher m = pattern.matcher(s);
int i = 0;
while (m.find()) {
i++;
}
return i;

}

/**
* 入口函数
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
RegXp rx = new RegXp();
int _char, _stmt, _word;
_char = rx.countChar(s);
_stmt = rx.countStatement(s);
_word = rx.countWord(s);
System.out.println("字符总数:" + _char);
System.out.println("句子总数:" + _stmt);
System.out.println("单词总数:" + _word);
}
}
相似回答