2016-10-22 13 views
1

私はこのプログラムを持っていますが、グローバル変数を削除する必要があります。物事を移動して部品を書き直そうとしていましたが、実際には分かりません。グローバル変数を削除するにはどうすればよいですか?

import java.util.Scanner; 

public class birds3 
{ 
    public static int maxIndex;                   //These three lines are the ones I'm talking about 
    public static String[] birds = new String[99999999]; 
    public static int[] numbers = new int[99999999]; 

    public static void main(String[] param) 
    { 
     whatBird(); 
     inputCheck(); 
     birdInput(); 
     System.exit(0); 
    } 

    public static void whatBird() 
    { 
     System.out.println("\nType 'END' to finish the program and display the most common bird. \n\nEnter anykey to continue."); 
     return; 
    } 

    public static String inputCheck() 
    { 
     Scanner scanner = new Scanner(System.in); 
     String input = scanner.nextLine(); 
     if (input.toUpperCase().equals("END")) 
     { 
      end(); 
     } 
     return input; 
    } 

    public static void birdInput() 
    { 
     int i = 0; 
     while (i <= birds.length) 
     { 
      System.out.println("\nWhat bird did you see?"); 
      birds[i] = inputCheck(); 
      System.out.println("\nHow many did you see?"); 
      numbers[i] = Integer.parseInt(inputCheck()); 
      i++; 
     } 
    } 

    public static int getMaxIndex(int[] numbers) 
    { 
     for (int i = 0; i < numbers.length; i++) 
     { 
      int newnumber = numbers[i]; 
      if ((newnumber > numbers.length)) 
      { 
       maxIndex = i; 
      } 
     } 
     return maxIndex; 
    } 

    public static void end() 
    { 
     maxIndex = getMaxIndex(numbers); 
     System.out.print("\nWell....I guess thanks for using this program?\n"); 
     System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n"); 
     System.exit(0); 
    } 
}  

私の問題を解決するためのヒントを教えていただけたら、どうかしてください。

EDIT:大域変数のいくつかを削除しましたが、まだ何かが機能していません。私が何を入力したとしても、どんな価値を持っていても、私が終わりに入ると、私は最初に入力したものと最初の値を印刷します。今問題は何ですか?

import java.util.Scanner; 

public class birds4 
{ 
    public static int maxIndex; 

    public static void main(String[] param) 
    { 
     String[] birds = new String[99999999]; 
     int[] numbers = new int[99999999]; 
     whatBird(); 
     inputCheck(birds, numbers); 
     birdInput(birds, numbers); 
     System.exit(0); 
    } 

    public static void whatBird() 
    { 
     System.out.println("\nType 'END' to finish the program and display the most common bird. \n\nEnter anykey to continue."); 
     return; 
    } 

    public static String inputCheck(String[] birds, int[] numbers) 
    { 
     Scanner scanner = new Scanner(System.in); 
     String input = scanner.nextLine(); 
     if (input.toUpperCase().equals("END")) 
     { 
      for (int i = 0; i < numbers.length; i++) 
      { 
       int newnumber = numbers[i]; 
       if ((newnumber > numbers.length)) 
       { 
        maxIndex = i; 
       } 
      } 
      System.out.print("\nWell....I guess thanks for using this program?\n"); 
      System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n"); 
      System.exit(0); 
     } 
     return input; 
    } 

    public static void birdInput(String[] birds, int[] numbers) 
{ 
     int i = 0; 
     while (i <= birds.length) 
     { 
      Scanner scanner = new Scanner(System.in); 
      System.out.println("\nWhat bird did you see?"); 
      birds[i] = inputCheck(birds, numbers); 
      System.out.println("\nHow many did you see?"); 
      numbers[i] = scanner.nextInt(); 
      i++; 
     } 
    } 
} 
+0

メソッドパラメータ。 – csmckelvey

答えて

3

あなたは「本当の」オブジェクトを使用して、代わりにすべての静的メソッドやフィールドを使用する方法について学ぶ必要があります。

もちろん、あなたのメインクラスはという静的で、コマンドラインから呼び出すことができるように、にする必要があります。しかし、あなたは通常次のようなことをします:

これは、OOP全体のポイントです。クラスは非静的なフィールドとメソッドを持っています。新しいオブジェクトを使用してオブジェクトをインスタンス化します。各オブジェクトは自身ののフィールドのコピーを持ちます(ただし、スタティックフィールドはすべてのこのようなオブジェクト間で共有されるため、「グローバル」なので、一般的には避けてください)。一例として

0

String[] birds = new String[99999999];int[] numbers = new int[99999999];がグローバルではありません、とbirdInput();によって必要とされている場合、あなたはで彼らの参照を渡す:

birdInput(birds,numbers); 

そしてbirdInput()の署名は次のように変更します。

public static void birdInput(String[] birds, int[] numbers) 

ロジックおよび/または戻り値を変更する必要がある場合があることに注意してください。

関連する問題