如何删除全局变量仍然可以使其工作?

时间:2016-10-22 11:37:16

标签: java

我有这个程序,但我需要删除全局变量。一直试图移动东西并重写部分,但我无法弄明白。

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);
    }
}    

如果你能给我一些如何解决我的问题的提示,请做。

编辑:好的,我已经删除了一些全局变量,但仍然无法正常工作。无论我输入什么或什么价值,如果我输入结束它总是只打印我输入的第一个东西和第一个值。现在问题是什么?

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++;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您必须了解使用“真实”对象,而不是使用所有静态方法和字段。

当然,您的主类需要静态,以便您可以从命令行调用它。但是你通常会这样做:

public class Example {
  private final String exampleString;

  public Example(String exampleString) { this.exampleString = exampleString; }

  public String getExampleString { return exampleString; }

  public static void main(String[] args) {
    Example myExample = new Example("whatever");
    System.out.println("The not so global example is: " + myExample.getExampleString());
  }
}

这就是OOP的重点:类具有非静态字段和方法;并使用new实例化对象;然后每个对象都有这些字段的自己的副本(而静态字段在所有这些对象之间共享;因此“全局”;因此通常应该避免它们)

答案 1 :(得分:0)

举例来说,如果String[] birds = new String[99999999];int[] numbers = new int[99999999];不是全局的,并且birdInput();需要,则您通过以下方式传递其参考:

 birdInput(birds,numbers);

birdInput()的签名应更改为:

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

请注意,在某些情况下,您需要更改逻辑和/或返回值。