首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有人能帮我纠正java pangram程序中的这个错误?

有没有人能帮我纠正java pangram程序中的这个错误?
EN

Stack Overflow用户
提问于 2019-11-28 17:35:04
回答 4查看 58关注 0票数 0

我的java代码有问题,但我无法检测到。我弄错了一个代码来检查pangram。一切看起来都很顺利,但我的代码无法编译和运行。下面是我得到的错误。pangram.Pangram.main(Pangram.java:29)处的线程"main“java.lang.ArrayIndexOutOfBoundsException: 42中出现异常

代码语言:javascript
运行
复制
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");

    }

}
EN

回答 4

Stack Overflow用户

发布于 2019-11-28 17:53:10

首先,i必须小于size,其次,不要在for循环中递增i,它会在每次迭代后递增。

一种可能的解决方案

代码语言:javascript
运行
复制
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");
票数 1
EN

Stack Overflow用户

发布于 2019-11-28 17:56:12

问题1:第22行

代码语言:javascript
运行
复制
   int indexOfLetter=x[i]-65;

似乎是不正确的,你得到输入字符串中的第一个字母,这只适用于大写字母,因为你使用的是整数表示。B和b有不同的整数表示,因此任何小写的-65都将大于26。要解决此问题,请在String sentence=scan.nextLine()之后添加.toUpperCase()

问题2:第25行

代码语言:javascript
运行
复制
       ++i;

此操作执行两次,一次是在for循环中

代码语言:javascript
运行
复制
for(i=0;i<=size;i++)

请删除++i;

问题3:第21行

代码语言:javascript
运行
复制
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‘

代码语言:javascript
运行
复制
        boolean foundZero = true;

    for (int a = 0; a < y.length; a++){
        if(y[a] == 0){
            foundZero = false;
        }
    }

    System.out.println(foundZero);

尽管这打印的是true false Hope,但这很有帮助!

票数 0
EN

Stack Overflow用户

发布于 2019-11-28 18:00:05

代码语言:javascript
运行
复制
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");
        }

    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59085635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档