我的java代码有问题,但我无法检测到。我弄错了一个代码来检查pangram。一切看起来都很顺利,但我的代码无法编译和运行。下面是我得到的错误。pangram.Pangram.main(Pangram.java:29)处的线程"main“java.lang.ArrayIndexOutOfBoundsException: 42中出现异常
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");
}
}
发布于 2019-11-28 17:53:10
首先,i必须小于size,其次,不要在for循环中递增i,它会在每次迭代后递增。
一种可能的解决方案
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a word or sentence:");
String sentence = scan.nextLine();
String sentence1 = sentence.toLowerCase().replace(" ", "");
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
for(int i=0; i < size; i++){
int indexOfLetter = x[i] - 'a';
//if you find the letter matching, replace 0 with 1 at that postion
y[indexOfLetter]=1;
}
//here check if its indeed a pangram
boolean pangram = true;
for (int i = 0; i < 26 && pangram; i++) {
if (y[i] == 0) {
pangram = false
}
}
System.out.println(pangram ? "YES" : "NO");
发布于 2019-11-28 17:56:12
问题1:第22行
int indexOfLetter=x[i]-65;
似乎是不正确的,你得到输入字符串中的第一个字母,这只适用于大写字母,因为你使用的是整数表示。B和b有不同的整数表示,因此任何小写的-65都将大于26。要解决此问题,请在String sentence=scan.nextLine()
之后添加.toUpperCase()
问题2:第25行
++i;
此操作执行两次,一次是在for循环中
for(i=0;i<=size;i++)
请删除++i;
问题3:第21行
for(i=0;i<=size;i++)
当循环遍历数组时,你应该在i < size
时结束,而不是在i <= size
时结束。这是因为数组从0开始,所以大小为4的东西的值为0,1,2,3。当你运行这个并让i <= size
时,你实际上尝试对输入数组中的4个元素运行5次for循环。
问题4:在末尾的while循环会导致无限循环。相反,只需检查数组是否包含0值,如果包含,则打印'no',而不打印'yes‘
boolean foundZero = true;
for (int a = 0; a < y.length; a++){
if(y[a] == 0){
foundZero = false;
}
}
System.out.println(foundZero);
尽管这打印的是true false Hope,但这很有帮助!
发布于 2019-11-28 18:00:05
you can do this way
package myproject.lambda;
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.toUpperCase().toCharArray();
// byte x[]=sentence1.toUpperCase().getBytes();
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;
}
// here check if its indeed a pangram
i = 0;
boolean isPangram = true;
for (int num : y) {
if (num == 0) {
isPangram = false;
break;
}
}
if (isPangram) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
https://stackoverflow.com/questions/59085635
复制相似问题