字符串数组无法正确打印

时间:2015-06-01 13:03:27

标签: java arrays

我尝试收到一条消息,其中所有a,b,c,de字符均由用户输入,如果条目不是所需条目,则会发送错误消息。但是当我以正确的顺序输入a,b,c,d,e,f时,它才能正常工作。但是,当我错过订单时,这不能正常工作。

请帮我解决这个问题。我认为错误在ifelse部分

public class Pangram {
    public static void main(String[] args) {
        System.out.print("your test : ");
        Scanner input = new Scanner(System.in);
        String[] array = new String[s.length() + 1];
        int[] alperbet = new int[27];
        String sub[] = s.split("");
        for (int i = 0; i <= s.length(); i++) {
            System.out.println(i + "th index" + sub[i]);
            array[i] = sub[i];
        }
        for (int i = 0; i < array.length; i++) {
            System.out.println("last array " + array[i]);
            if (array[i].equals("ksds")) {
                alperbet[0] = 1;
            } else if (array[i].equals("a")) {
                alperbet[1] = 1;
            } else if (array[i].equals("b")) {
                alperbet[2] = 1;
            } else if (array[i].equals("c")) {
                alperbet[3] = 1;
            } else if (array[i].equals("d")) {
                alperbet[4] = 1;
            } else if (array[i].equals("e")) {
                alperbet[5] = 1;
            }
        }
        for (int i = 0; i < alperbet.length; i++) {
            System.out.println(array[i] + " th index alphabet "
                    + alperbet[i]);
        /*  if (alperbet[i] == 0) {
                System.out.println("not pangram");
                break;
            } else {
                System.out.println("pangram");
                break;
            } */
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我在下面更正了您的代码。并添加了它的运行版本。

首先:清晰的格式化是良好编程的关键。

public class Pangram 
{
    public static void main(String[] args) 
    {
       System.out.print("your test : ");
       Scanner input = new Scanner(System.in);
       String[] array = new String[s.length() + 1];
       int[] alperbet = new int[27];
       // where did s come from?
       String sub[] = s.split("");

       // why not use sub? Why copying all to a second array?
       for (int i = 0; i <= s.length(); i++) 
       {
          System.out.println(i + "th index" + sub[i]);
          array[i] = sub[i];
       }

       for (int i = 0; i < array.length; i++) 
       {
           System.out.println("last array " + array[i]);
           // your split would never return a string "ksds"
           if (array[i].equals("a")) { alperbet[0] = 1; } 
           else if (array[i].equals("b")) { alperbet[1] = 1; } 
           else if (array[i].equals("c")) { alperbet[2] = 1; } 
           else if (array[i].equals("d")) { alperbet[3] = 1; } 
           else if (array[i].equals("e")) { alperbet[4] = 1; }
       }

       for (int i = 0; i < alperbet.length; i++) 
       {
          System.out.println(array[i] + " th index alphabet " + alperbet[i]);
          /*
          // You break the loop in each case, so you only check for 
          // alperbet[0]. There is no loop effectively.
          if (alperbet[i] == 0) 
          {
            System.out.println("not pangram");
            break;
          } 
          else 
          {
            System.out.println("pangram");
            break;
          } 
          */
       }
    }
}

我宁愿使用StringBuffer并迭代字符而不是字符串。但我认为这更接近于你所做的事情,因此更好地理解。

public class Pangram 
{
    public static void main(String[] args) 
    {
       boolean[] charFound = new boolean[5];
       // change the string to validate
       String sub[] = "abcdef".split("");

       for (int i = 0; i < sub.length; i++) 
       {
           if (sub[i].equals("a")) { charFound[0] = true; } 
           else if (sub[i].equals("b")) { charFound[1] = true; } 
           else if (sub[i].equals("c")) { charFound[2] = true; } 
           else if (sub[i].equals("d")) { charFound[3] = true; } 
           else if (sub[i].equals("e")) { charFound[4] = true; }
       }

       boolean flagOk = true;
       for (int i = 0; i < charFound.length; i++) 
       {
          if (!charFound[i]) 
          {
            flagOk = false;
            break;
          } 
       }
       if ( flagOk ) System.out.println("pangram");
       else System.out.println("not pangram");
    }

}

答案 1 :(得分:0)

尝试Java Api给出的Array.sort()函数。

答案 2 :(得分:0)

您是否尝试检查给定的字符数组是否包含某些字母? 如果是这样,您可以在字符串广告中使用contains()方法转换数组。

boolean check(char[] a){
    String s=new String(a);
    System.out.println(s);
    if(s.contains("a")&&s.contains("b")&&s.contains("c")&&s.contains("d")&&s.contains("e"))
        return true;
    else return false;
}