我用Java编写了一个潘格拉姆检测器。
一些测试案例给出了错误的答案,例如:“我们迅速判断古董象牙扣下一个奖项”。
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan=new Scanner(System.in);
String s=scan.nextLine();
s.toLowerCase();
if(s.length()<26){
System.out.println("not pangram");
System.exit(1);
}
char arr[]=s.toCharArray();
int counter=0;
char c='a';
for(c='a';c<='z';c++){
for(int i=0;i<arr.length;i++){
if(arr[i]==c){
counter++;
break;
}
}
}
if(counter==26){
System.out.println("pangram");
}
else{
System.out.println("not pangram");
}
}
}
发布于 2016-09-21 18:38:41
s.toLowerCase();
应该是
s = s.toLowerCase();
因为字符串是不可变的,所以toLowerCase()
返回一个新的String
,并且不更改原始的String
。
您的代码未能检测到输入"We promptly judged antique ivory buckles for the next prize"
的Pangram,因为您的循环测试了原始输入String
而不是它的小写版本,并且在原始String
中没有出现w
。
https://stackoverflow.com/questions/39624133
复制相似问题