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);
}
}
CheckWords(words);
显示错误,怎么回事?
大师, while(scanner.hasNext()) 怎么理解,没学过,求解释
追答读取屏幕所输入的单词,以空格为组!如how are you? scanner.hasNext()) 就是3次
本回答被提问者采纳