我的Java代码有问题,但我无法检测到。我错了一个代码来检查pangram。Eveything看起来很麻烦,但是我的代码无法编译和运行。这是我得到的错误。线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:42 at pangram.Pangram.main(Pangram.java:29)
package pangram;
import java.util.Scanner;
public class Pangram {
public static void main(String[] args) {
//take input from the user
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a word or sentence:");
String sentence=scan.nextLine();
//String sentence = "the quick brown fox jumps over lazy dog";
String sentence1=sentence.replace(" ", "");
//extract data from the object and place in an array x
char x[]=sentence1.toCharArray();
int size=x.length;
//initialize another array with zeros to fill the 26 positions of A-Z
int y[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//loop through the array using while loop
int i;
for(i=0;i<=size;i++){
int indexOfLetter=x[i]-65;
//if you find the letter matching, replace 0 with 1 at that postion
y[indexOfLetter]=1;
++i;
}
//here check if its indeed a pangram
i=0;
while(i!=26){ //26 is the number of letters in alphabet A-Z
if(y[i]==1){
i++;
}else{
System.out.println("NO");
}
}
System.out.println("YES");
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。