2014-12-05 59 views
0

我一直得到錯誤 「無法找到符號 符號:變量輸入 位置:類CountNumbers」在我的程序中,我已經把它放在整個程序。找不到符號,我不知道爲什麼

import java.util.Scanner; 

public class CountNumbers { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     char[] chars = createArray(); 

     System.out.println("The numbers are:"); 
     displayArray(chars); 
     int [] counts = countNumbers(chars); 

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

    public static char[] createArray() { 
     char[] chars = new char[100]; 

     for (int i = 0; i < chars.length; i++) 
     chars[i] = input.nextInt(); 

     return chars; 
     } 

    public static void displayArray (char[] chars) { 
     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 int[] countNumbers(char[] chars) { 
     int[] counts = new int[100]; 

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

     return 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.print(counts[i] + " " + (char)(i + 'a') + " "); 
     } 
    } 
} 

感謝您的幫助。

+0

你能發佈整個錯誤的堆棧跟蹤嗎? – msinghal 2014-12-05 03:21:58

+0

使用IDE,這種類型的錯誤不會打擾你。 – 2014-12-05 03:28:28

回答

2

input是主要方法的局部變量,然後嘗試在createArray方法中使用。如果你想在其他方法中使用input,它需要是成員或靜態變量。

或者對於您的情況,由於您只在createArray中使用input,因此可以將創建的input移動到createArray方法。

+0

好的,我把它移下來,現在我得到這個:找到1個錯誤: File:/ Volumes/IRON MAN/CS 1100/CountNumbers.java [line:23] Error:/ Volumes/IRON MAN/CS 1100/Count Number .java:23:可能丟失精度 找到:int 必需:字符 – 2014-12-05 03:48:12

+0

而當我改變,我得到1錯誤發現: 文件:/卷/ IRON MAN/CS 1100/CountNumbers.java [line:23] 錯誤:/ Volumes/IRON MAN/CS 1100/CountNumbers.java:23:找不到符號 symbol:method nextChar() location:類java.util.Scanner – 2014-12-05 03:49:34

相關問題