计算java中的字数
据我所知,1不是单词,单词的意思是一组字符的答案应该是2。1不应该被计算在内。 接受其他输入 输入:电脑应用12 现在,对于上面的输入,请回答单词3的频率,因为最后一个单词12是单词的意思计算java中的字数,java,Java,据我所知,1不是单词,单词的意思是一组字符的答案应该是2。1不应该被计算在内。 接受其他输入 输入:电脑应用12 现在,对于上面的输入,请回答单词3的频率,因为最后一个单词12是单词的意思 有两个字符。请不要使用拆分功能 您可以这样做: 1制作一个变量,该变量将在您遇到数字时被激活 Scanner sc=new Scanner(System.in); char ch; int W=0; String s; System.out.println("enter a
有两个字符。请不要使用拆分功能 您可以这样做: 1制作一个变量,该变量将在您遇到数字时被激活
Scanner sc=new Scanner(System.in);
char ch;
int W=0;
String s;
System.out.println("enter a String:");
s = sc.nextLine();
for(int i = 0;i<s.length();i++)
{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
W++;
}
System.out.println(W+1);
}
}
when i am executing above program with the
input :COMPUTER APPLICATION
its giving correct output no of words 2
in case if i entered
input: COMPUTER APPLICATION 1
answer should be for the no of words 2 but answer is displaying 3 program code also counts last 1.
在你的循环中,如果你一直面对一个数字,而不是一个有数字的单词:那么这将是真的
3最后,在您的条件下,只有当该变量为false时,才必须递增
boolean confront_number = false;
你的代码无法编译。请修复它并正确缩进代码。为了显示输入/输出,请使用blockquote,而不是代码来轻松区分它们。为什么不使用split方法?但是你得到三个单词的原因是你已经定义了1作为一个单词。而且,你所有的问题都被否决了,你有被禁止提问的风险:harshal parekh修复了错误,它仍然无法编译。
if(Character.isWhitespace(ch) && confront_number==false)
W++;