![](/img/trans.png)
[英]Java Letter changes arraylist to string without commas but including white spaces
[英]Count input without spaces, commas, or periods - Java
输入是“听着,琼斯先生,冷静下来。”
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
int numPun = 0; //number of punctiation and spaces
// Add more variables as needed
userText = scnr.nextLine(); // Gets entire line, including spaces.
for (int i = 0; i < userText.length(); i++){
if ((userText.charAt(i) != ' ') && (userText.charAt(i) != ',') && (userText.charAt(i) != '.'));{
numPun++;
}
}
System.out.println(numPun);
}
}
我现在的output是到29,也就是整行的字符总数。 预计 output 应该是 21。我哪里错了?
if
,并使用&&
NOT ||
在此答案的先前版本中,我指定使用||
而不是&&
。 不要那样做。 感谢Basil Bourque 的回答,因为他是第一个指出这一点的人。
利用:
for (int i = 0; i < userText.length(); i++){
char current = userText.charAt(i);
if (current != ' ' && current != ',' && current != '.'){
numPun++;
}
}
你有一个额外的;
在if
语句表达式之后。
看到它在行动
解释:
在此答案的先前版本中,表达式current,= ' ' || current.= ',' || current != '.'
current,= ' ' || current.= ',' || current != '.'
等于if current is not a space or is not a comma or is not a period do this:
.
这意味着如果字符是逗号,它将在numPun
中加 1,因为它不是空格。
在新表达式中,它等于if current is not a space and is not a comma and is not a period
。 因此它会首先检查它是否是一个空格,然后是逗号,然后是句点。
||
不正如Spectric 正确回答的那样,您需要修复杂散分号。
但是使用说明||
是不正确的。
这是您的代码,已更正。
String input = "Listen, Mr. Jones, calm down.";
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
//System.out.println( "c = " + c );
if ( ( c != ' ' ) && ( c != ',' ) && ( c != '.' ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
21
我会通过使用禁止字符List
而不是一系列相等测试来编写这样的代码。
String input = "Listen, Mr. Jones, calm down.";
List < String > nonLetters = List.of( " " , "," , "." );
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
System.out.println( "c = " + c );
if ( ! nonLetters.contains( String.valueOf( c ) ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
更好的是:使用代码点而不是char
类型。
char
char
类型在 Java 中已过时。 该类型是一个遗物,甚至无法表示Unicode中定义的字符的一半。
相反,使用代码点编号来表示每个字符。 Unicode 为其 143,859 个字符中的每一个分配一个 integer 编号。 代码点的范围从零到超过一百万,大多数未分配。
String
class 可以返回每个字符的代码点编号的IntStream
。 将该IntStream
转换为一个int
数组。 循环数组。 对于每个代码点编号,询问分配给该编号的字符 Unicode 是否被视为字母。 如果是一封信,请增加我们的计数。
String input = "Listen, Mr. Jones, calm down.";
int countLetters = 0;
int[] codePoints = input.codePoints().toArray();
for ( int codePoint : codePoints )
{
if ( Character.isLetter( codePoint ) )
{
countLetters++;
}
}
System.out.println( "countLetters = " + countLetters );
21
我们甚至可以使用 go 来制作单线 Java stream 和 Z945F3FC4495868ADB463 特征。
long countLetters =
"Listen, Mr. Jones, calm down."
.codePoints()
.filter(
( int codePoint ) -> Character.isLetter( codePoint )
)
.count()
;
21
(userText.charAt(i).= ' ') || (userText,charAt(i).= '.') || (userText.charAt(i) != '.')
此外,缺少 && (c.= '!')) ,它正在计算所有额外的感叹号并且未通过测试。
完成代码如下:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userText;
userText = scnr.nextLine();
String input = userText;
int countLetters = 0;
for ( int i = 0 ; i < input.length() ; i++ )
{
char c = input.charAt( i );
if ( ( c != ' ' ) && ( c != ',' ) && ( c != '.' ) && (c != '!' ) && (c !=
'!' ))
{
countLetters++;
}
}
System.out.println(countLetters );
}
}
通常,最好的第一步是简化问题:如果改写,它可以变成“有多少个单词字符?”,在代码中可以表示为“删除所有非单词字符后输入有多长时间:
int count = userText.replaceAll("\\W", "").length();
仅仅一行代码,隐藏的漏洞就更少了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.