System.out.Println问题

时间:2011-11-26 08:27:27

标签: java

这是用Java编写的。

这是一个两类程序(一个测试),它检查字符串中的元音并保持存在的数量。

除了第二类System.out.println() s(用于测试)之外,一切似乎都运行正常。

我之前从未遇到过这个问题,但我认为这只与打印线本身有关。

打印线位于底部。

以下是两个类:

/** Tests the sentence for the vowels
 * @ Jon Kosaka
 */

public class Sentence
{
    String sentence;

    public Sentence(String text)
    {
        sentence = text;
    }

    public boolean isPalindrome()
    {

        int low = 0;

        int high = sentence.length() -1;

        boolean match = true;

        while (low < high && match)
        {
            if (sentence.charAt(low) != sentence.charAt(high))
            {
                match = false;
            }

            low++;
            high--;
        }
        return match;
    }

    public String getSentence()
    {
        return sentence;
    }

    public void setSentence(String newText)
    {
        sentence = newText;
    }


    public int[] getVowelCounts()
    {

        final int A_INDEX = 0;
        final int E_INDEX = 0;
        final int I_INDEX = 0;
        final int O_INDEX = 0;
        final int U_INDEX = 0; 

        //              a  e  i  o  u
        int[] counts = {A_INDEX, E_INDEX, I_INDEX, O_INDEX, U_INDEX};
        int i;
        for (i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(0) == 'a')
                counts[0]++;
            if (sentence.charAt(0) == 'e')
                counts[1]++;
            if (sentence.charAt(0) == 'i')
                counts[2]++;
            if (sentence.charAt(0) == 'o')
                counts[3]++;
            if (sentence.charAt(0)  == 'u')
                counts[4]++;

        }
        return counts;
    }


}

其他课程:

import java.util.Scanner;
/**
  An application to count the number of each individual vowel
  @ Jon Kosaka
 */

public class WorkingWithStrings
{

    public static void main(String[] args)
    {
        //create a scanner
        Scanner console = new Scanner(System.in);

        //get the input
        System.out.println("Enter a sentence and I will count each vowel: ");
        String input = console.nextLine();

        //Create a vowelCounter and call method to count the vowels
        Sentence sentence = new Sentence(input);
        int[] counts = sentence.getVowelCounts();
    }
    //display the results
    System.out.println("Vowel Count for the sentence: ");
    System.out.println("      " + sentence.getSentence());
    System.out.println("number of a: " + counts[Sentence.A_INDEX]);
    System.out.println("number of e: " + counts[Sentence.E_INDEX]);
    System.out.println("number of i: " + counts[Sentence.I_INDEX]);
    System.out.println("number of o: " + counts[Sentence.O_INDEX]);
    System.out.println("number of u: " + counts[Sentence.U_INDEX]);

}

6 个答案:

答案 0 :(得分:2)

您的某些陈述不属于任何方法。这不会编译。

import java.util.Scanner;
/**
   An application to count the number of each individual vowel
   @ Jon Kosaka
*/

public class WorkingWithStrings
{

  public static void main(String[] args)
  {
    //create a scanner
    Scanner console = new Scanner(System.in);

    //get the input
    System.out.println("Enter a sentence and I will count each vowel: ");
    String input = console.nextLine();

    //Create a vowelCounter and call method to count the vowels
    Sentence sentence = new Sentence(input);
    int[] counts = sentence.getVowelCounts();
    //}  DELETED HERE
    //display the results
    System.out.println("Vowel Count for the sentence: ");
    System.out.println("      " + sentence.getSentence());
    System.out.println("number of a: " + counts[Sentence.A_INDEX]);
    System.out.println("number of e: " + counts[Sentence.E_INDEX]);
    System.out.println("number of i: " + counts[Sentence.I_INDEX]);
    System.out.println("number of o: " + counts[Sentence.O_INDEX]);
    System.out.println("number of u: " + counts[Sentence.U_INDEX]);
  } //ADDED HERE
}

答案 1 :(得分:2)

有很多错误:)

1:

要查找和增加您的元音数量:

for (i = 0; i < sentence.length(); i++) {
 if (sentence.charAt(0) == 'a')
     counts[0]++;
 ...

在每次迭代中检查索引0处的字符。所以

if (sentence.charAt(0) == 'a')

应该是

if (sentence.charAt(i) == 'a')

2:

您在}方法中错误放置了main,因此某些代码超出了任何功能。解决了这个问题。

3:

常量A_INDEXB_INDEX ...是一个类中的本地方法,您从另一个类访问它们。您需要将它们设为Sentence类的静态变量。你也已经为它们分配了值0,因为它们被用作保存元音数的数组中的索引。所以你需要:

static final int A_INDEX = 0;
static final int E_INDEX = 1;
static final int I_INDEX = 2;
static final int O_INDEX = 3;
static final int U_INDEX = 4; 

4:

变化:

int[] counts = {A_INDEX, E_INDEX, I_INDEX, O_INDEX, U_INDEX};

int[] counts = {0,0,0,0,0};

Here is a working program

答案 2 :(得分:1)

我发现有两个问题:

  1. 您的System.out.println()位于主要功能
  2. 之外
  3. Sentence.A_INDEX等不是静态类变量,它们是在成员函数中声明的

答案 3 :(得分:1)

您必须将System.out.print放在public static void main

公共类WorkingWithStrings     {

   public static void main(String[] args)
   {
      //create a scanner
      Scanner console = new Scanner(System.in);

      //get the input
      System.out.println("Enter a sentence and I will count each vowel: ");
      String input = console.nextLine();

      //Create a vowelCounter and call method to count the vowels
      Sentence sentence = new Sentence(input);
      int[] counts = sentence.getVowelCounts();

//display the results
      System.out.println("Vowel Count for the sentence: ");
      System.out.println("      " + sentence.getSentence());
      System.out.println("number of a: " + counts[Sentence.A_INDEX]);
      System.out.println("number of e: " + counts[Sentence.E_INDEX]);
      System.out.println("number of i: " + counts[Sentence.I_INDEX]);
      System.out.println("number of o: " + counts[Sentence.O_INDEX]);
      System.out.println("number of u: " + counts[Sentence.U_INDEX]);
  }


}

答案 4 :(得分:0)

在不知道确切问题的情况下,这里有一些怀疑:

counts[Sentence.A_INDEX]等引用了A_INDEX中不存在的静态字段Sentence。 如果它作为静态变量存在,那么所有这些仍然会被初始化为0,因此counts[Sentence.A_INDEX]counts[Sentence.E_INDEX]都会被转换为counts[0]

答案 5 :(得分:0)

您的代码中存在一些问题我认为这个问题是您想要的正确/正确的代码。

 public class Sentence
{
   String sentence;

   public Sentence(String text)
   {
      sentence = text;
   }

   public boolean isPalindrome()
   {

      int low = 0;

      int high = sentence.length() -1;

      boolean match = true;

       while (low < high && match)
       {
          if (sentence.charAt(low) != sentence.charAt(high))
          {
             match = false;
          }

          low++;
          high--;
       }
       return match;
   }

   public String getSentence()
   {
      return sentence;
   }

   public void setSentence(String newText)
   {
      sentence = newText;
   }


   public int[] getVowelCounts()
   {

       final int A_INDEX = 0;
       final int E_INDEX = 0;
       final int I_INDEX = 0;
       final int O_INDEX = 0;
       final int U_INDEX = 0; 

//              a  e  i  o  u
int[] counts = {A_INDEX, E_INDEX, I_INDEX, O_INDEX, U_INDEX};
int i;
for (i = 0; i < sentence.length(); i++) {
 if (sentence.charAt(i) == 'a')
     counts[0]++;
 if (sentence.charAt(i) == 'e')
     counts[1]++;
 if (sentence.charAt(i) == 'i')
     counts[2]++;
 if (sentence.charAt(i) == 'o')
     counts[3]++;
 if (sentence.charAt(i)  == 'u')
     counts[4]++;

}
return counts;
   }


}




import java.util.Scanner;
/**
   An application to count the number of each individual vowel
   @ Jon Kosaka
*/

public class WorkingWithStrings
{

   public static void main(String[] args)
   {
      //create a scanner
      Scanner console = new Scanner(System.in);

      //get the input
      System.out.println("Enter a sentence and I will count each vowel: ");
      String input = console.nextLine();

      //Create a vowelCounter and call method to count the vowels
      Sentence sentence = new Sentence(input);
      int[] counts = sentence.getVowelCounts();

//显示结果

      System.out.println("Vowel Count for the sentence: ");
      System.out.println("      " + sentence.getSentence());
      System.out.println("number of a: " + counts[0]);
      System.out.println("number of e: " + counts[1]);
      System.out.println("number of i: " + counts[2]);
      System.out.println("number of o: " + counts[3]);
      System.out.println("number of u: " + counts[4]);

  }

}