Java阵列和扫描仪帮助 [英] Java array and scanner Help

查看:96
本文介绍了Java阵列和扫描仪帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那里
我有一个场景,一个野生动物观察员正在寻找野生动物.观察者必须根据看到的动物输​​入1-8的数字.他一次输入一个数字,就知道哪个数字代表每只动物,看起来像这样,
1
4
3
3
1
8
4
2
3
等等
我需要制作一个包含7只动物的阵列.河马,猎豹,水牛,猴子,斑马,老虎,蛇和犀牛

我需要将这些动物附加到数组1-8.
然后我想使用扫描仪工具,以便用户可以根据他看到的动物随机输入数字.
同样,阵列中的每个动物都需要一个计数器,因此它可以增加观察者看到特定动物的次数.那么,如果用户按下0,则将得出数据摘要,例如,犀牛总数= 20,老虎总数为10

hi there
I have a scenario where a wildlife observer is looking for animals in the wild. The observer must enter a number 1-8 depending on the animal he sees. he enters the numbers one at a time and he knows which number represents each animal, which could look like this,
1
4
3
3
1
8
4
2
3
and so on
I need to make an array with 7 animals. hippo, cheetah, buffalo, monkey, zebra, tiger, snake and Rhino

I need to attach these animals to an array 1 - 8.
then I want to use the scanner tool so the user can enter the numbers in at random according to what animal he sees.
Also each animal in the array needs a counter so it can increment how many times the observer sees the particular animal. then if the user presses 0 a summary of data will come up for e.g total amount of rhinos = 20 and the total amount of tigers is 10

推荐答案

简单方式:
Simples:
public class AnimalCount {
    /**
      * @param args
     */
    public static void main(String[] args) {
        System.out.println("Welcome to animal count Simulator");
        System.out.println("Please enter values between 1 and 8 depending on the animal (0 will exit the input and display a Summary of data)");

        int userNumber;
        int highest = 0;
        int lowest = 0;
        int total = 0;
        int[] animals = new int[8];
        for (int animal = 0;
                animal < 8;
                animal++) {
            animals[animal]=0;
        }
        Scanner scanner = new Scanner(System.in);
        userNumber = scanner.nextInt();
        while (userNumber > 0) {
            int animal = userNumber - 1; // remove the offset
            if (animal < 8) {
                total++;
                animals[animal]++;
            }
            userNumber = scanner.nextInt();
        }
        
        // now check the most and least
        // starts with 0 being most and least
        for (int animal = 1;
                animal < 8;
                animal++) {
            if (animals[animal] > animals[highest]) {
                highest = animal;
            }
            if (animals[animal] < animals[lowest]) {
                lowest = animal;
            }
        }        
    }
}


这篇关于Java阵列和扫描仪帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆