如何计算 java 中字符串中的标点符号?
How to count punctuation marks in a String in java?
我需要编写一个程序来计算字符串中的某些标点符号。我有这段我认为可行的代码,但在每个字符处,我都有一个错误,指出赋值的左手必须是一个变量。需要帮助解决此问题
public static void main(String[] args)
{
Scanner kbd = new Scanner (System.in);
System.out.println("Enter a string: ");
String s = kbd.next();
countPunctuation(s);
}
public static int countPunctuation(String s)
{
int periodCount = 0;
int commaCount = 0;
int semicolonCount = 0;
int colonCount = 0;
int exclamationCount = 0;
int questionCount = 0;
int total = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) = ".")
{
periodCount++;
total++;
}
if(s.charAt(i) = ",")
{
commaCount++;
total++;
}
if(s.charAt(i) = ";")
{
semicolonCount++;
total++;
}
if(s.charAt(i) = ":")
{
colonCount++;
total++;
}
if(s.charAt(i) = "!")
{
exclamationCount++;
total++;
}
if(s.charAt(i) = "?")
{
questionCount++;
total++;
}
}
System.out.println("There are " + periodCount + " periods in this String.");
System.out.println("There are " + commaCount + " commas in this String.");
System.out.println("There are " + semicolonCount + " semicolons in this String.");
System.out.println("There are " + colonCount + " colons in this String.");
System.out.println("There are " + exclamationCount + " exclamation marks in this String.");
System.out.println("There are " + questionCount + " quesiton marks in this String.");
return total;
}
相等性检查是 ==
而不是 =
。你需要有类似 if(s.charAt(i) == '.')
的东西,或者你可以在这种情况下使用开关:
for(int i = 0; i < s.length(); i++) {
switch(s.charAt(i)) {
case '.':
periodCount++;
break;
case ',':
commaCount++;
break;
... // similar conditions for others
}
total += 1;
}
不知道这是否有帮助,但我发现了一个小错误
变化:
String s = kbd.next();
收件人:
String s = kbd.nextLine();
我认为这绝对应该解决它。
为什么?:
next(); -is mostly used for characters, as it doesn't get the whole line.
nextLine(); -gets the entire line, which for your code, you'd want.
就像那个人说的,你正在使用
= -instead of- ==
"=" 用于分配的变量和东西
“==”是检查,比如 if (String s == ";")
我需要编写一个程序来计算字符串中的某些标点符号。我有这段我认为可行的代码,但在每个字符处,我都有一个错误,指出赋值的左手必须是一个变量。需要帮助解决此问题
public static void main(String[] args)
{
Scanner kbd = new Scanner (System.in);
System.out.println("Enter a string: ");
String s = kbd.next();
countPunctuation(s);
}
public static int countPunctuation(String s)
{
int periodCount = 0;
int commaCount = 0;
int semicolonCount = 0;
int colonCount = 0;
int exclamationCount = 0;
int questionCount = 0;
int total = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) = ".")
{
periodCount++;
total++;
}
if(s.charAt(i) = ",")
{
commaCount++;
total++;
}
if(s.charAt(i) = ";")
{
semicolonCount++;
total++;
}
if(s.charAt(i) = ":")
{
colonCount++;
total++;
}
if(s.charAt(i) = "!")
{
exclamationCount++;
total++;
}
if(s.charAt(i) = "?")
{
questionCount++;
total++;
}
}
System.out.println("There are " + periodCount + " periods in this String.");
System.out.println("There are " + commaCount + " commas in this String.");
System.out.println("There are " + semicolonCount + " semicolons in this String.");
System.out.println("There are " + colonCount + " colons in this String.");
System.out.println("There are " + exclamationCount + " exclamation marks in this String.");
System.out.println("There are " + questionCount + " quesiton marks in this String.");
return total;
}
相等性检查是 ==
而不是 =
。你需要有类似 if(s.charAt(i) == '.')
的东西,或者你可以在这种情况下使用开关:
for(int i = 0; i < s.length(); i++) {
switch(s.charAt(i)) {
case '.':
periodCount++;
break;
case ',':
commaCount++;
break;
... // similar conditions for others
}
total += 1;
}
不知道这是否有帮助,但我发现了一个小错误
变化:
String s = kbd.next();
收件人:
String s = kbd.nextLine();
我认为这绝对应该解决它。
为什么?:
next(); -is mostly used for characters, as it doesn't get the whole line.
nextLine(); -gets the entire line, which for your code, you'd want.
就像那个人说的,你正在使用
= -instead of- ==
"=" 用于分配的变量和东西 “==”是检查,比如 if (String s == ";")