简体   繁体   English

java - 如何在java中向用户显示选定的数组内容?

[英]How can I display chosen array contents to the user in java?

I have created a random number generator and I also have an array of 30 items, however I require it to use the numbers generated to get the positions of the items in the array and display it.我创建了一个随机数生成器,我还有一个包含 30 个项目的数组,但是我要求它使用生成的数字来获取数组中项目的位置并显示它。 Sorry if I don't make much sense, I'm still trying to understand it myself!对不起,如果我没有多大意义,我仍然试图自己理解它!

Here's my random number generator code!这是我的随机数生成器代码!

public void passRndNum() throws IOException public void passRndNum() 抛出 IOException

    ArrayList <Integer> aNumber = new ArrayList <Integer>();

    for (int i = 0; i < 29; i++)
    {
        aNumber.add(new Integer(i));
    }
    Collections.shuffle(aNumber);
    for (int i = 0;i < 2 ; i++)
    {
        aPlayer[i].receiveCard(aNumber.get(i));
    }

}

Is it possible to create random number between 0 and 29是否可以创建 0 到 29 之间的随机数

Yes.是的。

Random rnd = new Random();
rnd.nextInt(30);

to represent a counter代表一个计数器

Yes.是的。

int counter = rnd.nextInt(30);

of an array of 30 items包含 30 个项目的数组

Yes, you can have an array of 30 items.是的,您可以拥有 30 个项目的数组。 Lets call it ... myArray .让我们称之为... myArray

and then call that counter and item to the display?然后将该计数器和项目调用到显示器上?

Yes.是的。

System.out.println(counter + ": " + myArray[counter]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM