输出将无法打印正确的信息

时间:2016-10-18 12:07:26

标签: java

我正在尝试制作一个程序,询问用户特定的鸟,然后他们在那一点看到了多少鸟。如果在任何时候使用单词'END',那么系统应该打印出最常见的鸟和所看到的数字。但是,当我运行我的程序时,如果我在随机点输入'END',它会返回最常见的是END,看到0。我无法弄清楚如何让它发挥作用。我尝试了不同的方法,但它没有正常工作。此外,我已将最大数组限制设置为10个,但是在10之后它会继续,如果我输入一个值,系统会崩溃。我是否正确写了极限部分?或者我错过了一些重要的东西?

import java.util.Scanner;

public class testing
{
    public static void main (String[] param)
    {
        birdInput();
        most();
        System.exit(0);
    }

    public static void birdInput()
    {       
        int i = 0;
        String birdInput;
        int numberInput;
        Scanner scanner = new Scanner(System.in);
        int maxVal = Integer.MIN_VALUE;
            int maxValIndex = -1;
        while (true) 
        {
            System.out.println("What bird did you see?");
            birdInput = scanner.nextLine();
            if (birdInput.equals("END"))
            {
                System.out.print("\nWell....I guess thanks for using this program?\n");
                System.exit(0);
            }
            else
            {
                String[] birds = new String[10];
                int[] numbers = new int[10];
                birds[i] = scanner.nextLine();
                System.out.println("How many did you see?");
                numbers[i] = scanner.nextInt();
                i++;
                if (birds[i].equals("END"))
                {
                    maxVal = numbers[i];
                    maxValIndex = i;
                    System.out.print("\nThe most common bird that you saw was the " + birds[maxValIndex] + " with " + maxVal + " being seen in total\n");
                    System.exit(0);
                }
            }
        }
    }
    public static void most()
    {
        System.out.println("fdff");
    }
} 

这是我对Till Hemmerich对我的问题的回答的编辑。我试图删除全局变量,因此将整个代码合并为1个方法。但是,我还有一些问题。一直在努力,但真的很困惑。

import java.util.Scanner;

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

    public static void birdInput()
    {
        Scanner scanner = new Scanner(System.in);
        String[] birds = new String[99999999];
        int[] numbers = new int[99999999];
        int i = 0;
        int maxIndex;
        while (i <= birds.length)
        {
            System.out.println("What bird did you see?");
            birds[i] = scanner.nextLine();
            System.out.println("How many did you see?");
            numbers[i] = scanner.nextInt();
            i++;
        }
        int newnumber = numbers[i];
        if ((newnumber > numbers.length))
        {
            maxIndex = i;
            i++;
        }
        if (birds[i].toUpperCase().equals("END"))
        {
            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);
        }
    }
}    

2 个答案:

答案 0 :(得分:2)

您在循环的每次迭代中重新声明birdsnumbers数组。它们应该在循环之前声明和初始化一次。

答案 1 :(得分:0)

  

我改变了很多,所以我要在这里解释我的变化。

     

首先,我必须将数组定义移出你的while循环,如上所述,因为其他方面你每次都会覆盖这些数组。   我还让他们在全球范围内可以通过其他方法与他们合作。

    public static int maxIndex;
    public static String[] birds = new String[10];
    public static int[] numbers = new int[10];
  

总的来说,我重新构造了整个代码,使其更具可读性,并且更加面向对象。   例如,我创建了一个名为inputCheck()的方法,它将我们的输入作为String返回,并检查它是否等于END,因此您不必为此编写两次逻辑。 (它还考虑在检查之前将输入结束较低或大写为高位输入&#34; if(input.toUpperCase()。equals(&#34; END&#34;))&#34;)

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

现在每次需要这样的输入时都可以调用此方法:

birds[i] = inputCheck();
  

但是你需要小心,如果你想得到一个整数,你首先必须像这样解析它:Integer.parseInt(inputCheck())   之后我写了一个方法来搜索数字数组中最大的值并得到它的索引:

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;

}
  

它将一个int数组作为参数,并返回其中最高元素的索引作为整数。这样称呼:maxIndex = getMaxIndex(numbers);

     

然后我重写了你的结束方法。它现在只调用我们的getMaxIndex方法并将一些输出打印到控制台。

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

}
  

修复你的上一个问题(超过10个输入后崩溃)我改变了你的while循环。如果您尝试将信息放在第11位,那么您的数组只有10个位置可以放置它崩溃。它看起来不像这样:while (i <= birds.length)而不是while (true)这样它可以采取的最大循环是阵列鸟类的地方很多,它不会再崩溃了。

public static void birdInput() {
        int i = 0;
        while (i <= birds.length) {
            System.out.println("What bird did you see?");
            birds[i] = inputCheck();
            System.out.println("How many did you see?");
            numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash 
            i++;

        }
    }
  
    

以下是整个代码:

  
import java.util.Scanner;

/ **  *  * @author E0268617  * / 公共类JavaApplication1 {

    public static int maxIndex;
    public static String[] birds = new String[10];
    public static int[] numbers = new int[10];

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

    public static void birdInput() {
        int i = 0;
        while (i <= birds.length) {
            System.out.println("What bird did you see?");
            birds[i] = inputCheck();
            System.out.println("How many did you see?");
            numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash 
            i++;

        }
    }

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


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

}

public static void most() {
    System.out.println("fdff");
}
}
  

如果您有任何问题,我希望您了解问题被隐藏的位置。