私はこのプログラムを持っていますが、グローバル変数を削除する必要があります。物事を移動して部品を書き直そうとしていましたが、実際には分かりません。グローバル変数を削除するにはどうすればよいですか?
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++;
}
}
}
メソッドパラメータ。 – csmckelvey