2013-12-13 4 views
0

나는 자바를 배우고 있는데,이 코드에 버그가있다. 나는 그것을 고치는 법을 모른다. 여기 버그 자바. 나는 그것을 고치는 방법을 모른다.

코드입니다 :

public class CountLettersInArray { 

    public static void main(String[] args) { 
    char[] chars = createArray(); 

    System.out.println("The lowercase letters are:"); 
    displayArray(chars);  

    int[] counts = countLetters(chars); 

    System.out.println(" "); 
    System.out.println("The occurence of each letter are: "); 
    displayCounts(counts); 
    } 

    public static void displayCounts(int[] counts) { 
     for (int i = 0; i < counts.length; i++); 
      if ((i + 1) % 10 == 0); 
       System.out.println(counts[i] + " " + (char)(i + 'a')); 
       else 
        System.out.println(counts[i] + " " + (char)(i + 'a') + " "); 


    } 

    public static int[] countLetters(char[] chars) { 
     //Declare and create an array of 26 int 
     int[] counts = new int[26]; 

     //For each lowercase letter in the array, count it 
     for (int i = 0; i < chars.length; i++); 
      counts[chars[i] - 'a']++; 

     return counts; 
    } 

    public static void displayArray(char[] chars) { 
     //Display the characters in the array 20/line 
     for (int i = 0; i < chars.length; i++); 
      if ((i + 1) % 20 == 0) 
       System.out.println(chars[i]); 
      else 
       System.out.print(chars[i] + " "); 
    } 

    public static char[] createArray() { 
     //Declare the array of characters and create it 
     char[] chars = new char[100]; 

     //Create lowercase characters randomly and assign them to array 
     for (int i = 0; i < chars.length; i++); 
      chars[i] = RamdomCharacter.getRandomLowerCaseLetter(); 
     //This return the array 
     return chars; 
    } 

} 

내가 Eclypse로 코딩하고있어 소프트웨어는 나에게 그 두 가지를 말하고있다 : 나는이 문제를 해결하기 위해 supose하고 어떻게

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    i cannot be resolved to a variable 
    RamdomCharacter cannot be resolved 

를?

for (int i = 0; i < counts.length; i++); 
            ^

은 그 없애 및 {}와 루프 시체를 둘러싸고 :

+0

Eclipse가 말했듯이 RamdomCharacter 클래스는 존재하지 않습니다. 프로젝트에있는 경우이 클래스의 맨 위에 가져와야합니다. 그리고 각각을 위해 다음과 같이 써야합니다 : for (...) {actions; } – Kloe2378231

+0

이클립스에서'Ctrl + Shift + F' 키를 눌러 코드가 실제로 어떻게 보이는지 볼 수있다. (예를 들어 for (')'의 뒤에'; 'RamdomCharacter'도 무엇입니까? * Ra ** n ** domCharacter *에 액세스 할 수 있습니까? – Pshemo

+0

루프와'if/else' 구문에 중괄호 ('{'와'}')를 사용하는 방법을 배웁니다. – GriffeyDog

답변

1

을 말합니다. 클래스는 RamdomCharacter입니다.

  1. 나는 당신이 당신의 프로젝트에 이러한 클래스가 있나요 RandomCharacter
  2. 을 의미 생각?
2

당신은 루프의 끝에서 ;의 려구요.

지금 문제는 i이 존재한다는 것입니다. 이 루프의 범위에 있습니다. 그러나 ;을 추가하여 루프 범위를 종료 했으므로 i을 참조하면 컴파일 오류가 발생합니다.

0
for (int i = 0; i < chars.length; i++); <--- remove the ; 
     counts[chars[i] - 'a']++; 

;은 성명을 종료합니다. 따라서 counts[chars[i] - 'a']++;은 예상대로 for 루프에 캡슐화되지 않습니다. 따라서 변수 i에 액세스 할 수 없습니다.

  • 당신은 당신의 루프
0

캡슐화 같은 일을 또 다른 두 번

  • 사용 괄호 {}을 수행 한 RamdomCharacter 클래스가 정의 된 위치를 두 번째 문제를 들어, 내가 볼 수없는, 하지만 내 생각 엔 실제로는 RandomCharacter라고하며, n

  • 0

    다른 두 가지로 지적 된 문제를 제외하면 RamdomCharacter가 제대로 가져 오지 않은 것 같습니다. 그게 왜 그런 오류가 발생하는지. 클래스를 올바르게 임포트하십시오.

    관련 문제