java 编写一个程序来计算短语中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5223381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a program to count spaces in a phrase
提问by Tuan Nguyen
I'm trying to write a program where a user would enter a phrase, and the program would count the blank spaces and tell the user how many are there. Using a for loop but i'm stuck, could someone help me out?
我正在尝试编写一个程序,让用户输入一个短语,该程序会计算空格数并告诉用户有多少空格。使用 for 循环但我卡住了,有人可以帮我吗?
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();
// Read in a string and find its length
System.out.print ("Enter a sentence or phrase: ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
for(ch=phrase.charAt()
// and count the blank spaces
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ();
}
}
回答by ChrisJ
The for
loop for counting spaces would be written as follows:
for
计算空格的循环将写成如下:
for(int i=0; i<phrase.length(); i++) {
if(Character.isWhitespace(phrase.charAt(i))) {
countBlank++;
}
}
It reads as follows: “i
is an index, ranging from the index of the first character to the index of the last one. For each character (gotten with phrase.charAt(i)
), if it is whitespace (we use the Character.isWhitespace
utility function here), then increment the countBlank
variable.”
其内容如下:“i
是一个索引,范围从第一个字符的索引到最后一个字符的索引。对于每个字符(用 获得phrase.charAt(i)
),如果它是空格(我们在Character.isWhitespace
这里使用效用函数),则增加countBlank
变量。”
回答by mellamokb
- Loop through the characters in the string.
- Check if the character is a space (char value =
32
orch == ' '
) - If space, add to countBlank, otherwise continue
- Display the results.
- 循环遍历字符串中的字符。
- 检查字符是否为空格(字符值 =
32
或ch == ' '
) - 如果有空格,加到countBlank,否则继续
- 显示结果。
You might look at the Stringand Characterclasses in the Java documentation for assistance.
回答by oddi
I'm not very familiar with java, but if you can access each character in the string. You could write something like this.
我对java不是很熟悉,但是如果您可以访问字符串中的每个字符。你可以写这样的东西。
int nChars = phrase.length();
for (int i = 0; i < nChars; i++) {
if (phrase.charAt(i) == ' ') {
countBlank++;
}
}
回答by Ta01
Just wondering, couldn't you just split the string entered by blank spaces and take the length of the array subtracted by 1?
只是想知道,您不能将空格输入的字符串拆分并取数组的长度减1吗?
In C# it would be as trivial as
在 C# 中,它会像
string x = "Hello Bob Man";
int spaces = x.Split(' ').Length - 1;
Pretty sure java has a split? Works even if you have two contiguous spaces.
很确定java有分裂?即使你有两个连续的空间也能工作。
回答by Oleg Shanyuk
You have to complete for cycle and count spaces
你必须完成循环和计数空间
//replace this lines
for(ch=phrase.charAt()
// and count the blank spaces
//to this lines
for (int i = 0; i < phrase.length(); i++)
{
if(phrase.charAt(i) == ' ') countBlank++;
}
回答by Damian Leszczyński - Vash
You have probably problem with that for each loop
您可能对每个循环都有问题
char[] chars = phrase.toCharArray(); Change string into array of chars.
for(char c : phrase.toCharArray()) { //For each char in array
if(Character.isWhitespace(c) { //Check is white space.
countBlank++; //Increment counter by one.
}
}
or
或者
for(int i =0; i <phrase.lenght(); i++) {
if(Character.isWhitespace(phrase.charAt(i)) { //Check is the character on position i in phrase is a white space.
countBlank++; //Increment counter by one.
}
}
回答by blong824
This is at the following Java Tutorials
这是在以下Java 教程中
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class SplitDemo2 {
private static final String REGEX = "\d";
private static final String INPUT = "one9two4three7four1five";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
String[] items = p.split(INPUT);
for(String s : items) {
System.out.println(s);
}
}
}
OUTPUT:
one
two
three
four
five
The regex for whitespace is \s
空格的正则表达式是 \s
Hope that helps.
希望有帮助。