在方法中生成随机数(generate random numbers in methods)

我做了代码,在1到100之间做一个随机数作为任务A),然后我如何理解,如果超过50的第一个值产生1到50之间的第二个随机数,如所述(我认为)任务B)

请任何人都可以解释C和D的任务是什么,不知道怎么做。(((请帮助建议或解释任务C和D.

谢谢....

任务:

编写一个生成随机数的程序:

a)+编写一个返回1到100范围内随机整数的方法。

b)然后添加另一个方法,该方法采用指定顶部数字的参数 - 即如果你传递它50它返回1到50之间的随机数。测试你的随机方法并确保它有效。

c)编写另一种方法,以便传递两个值 - 你希望得到最高值的范围的顶部和底部 - 即如果你传递10和20,它将返回10到20之间的随机数。

d)然后写另一个方法,如果反复调用它不会返回两个相同的数字? 因此,一旦生成并返回了随机数,该方法就不会再次返回该数字。 为此,您必须存储生成的每个数字。

import java.util.Random; public class ranGen { public Integer random (Integer integer){ Random rand = new Random();; int min=0, max=100; int randomNum = rand.nextInt(max - min + 1) + min; System.out.println(randomNum); return randomNum; } public void random50 (Integer integer){ Random rand = new Random();; int min=0, max=50; int randomNum = rand.nextInt(max - min +1) + min; System.out.println(randomNum); } public static void main(String[] args) { ranGen process = new ranGen(); if(process.random(null) > 50){ process.random50(null); } } }

I did the code, make a random number between 1 and 100 as task A), then how I understood if the first value if more than 50 generate second random number between 1 and 50 as said ( how I think) task B)

PLEASE can any one explain what the task C and D is, not understand how to do it at all ..(((pls help with advice or explanation task C and D.

Thanks....

TASK:

Write a program that generates random numbers:

a) +Write a method that returns a random integer in the range of 1 to 100.

b) Then add another method that takes a parameter specifying the top number – i.e. if you pass it 50 it returns a random number between 1 and 50. Test your random method and make sure it works.

c) Write another method so that you pass it two values – the top and bottom of the range you want the highest value from – i.e. if you pass it 10 and 20 it returns a random number between 10 and 20.

d) Then write another method so that if repeatedly called it doesn’t return two numbers the same? So once a random number has been generated and returned, the method doesn’t return that number again. To do this you will have to store every number generated.

import java.util.Random; public class ranGen { public Integer random (Integer integer){ Random rand = new Random();; int min=0, max=100; int randomNum = rand.nextInt(max - min + 1) + min; System.out.println(randomNum); return randomNum; } public void random50 (Integer integer){ Random rand = new Random();; int min=0, max=50; int randomNum = rand.nextInt(max - min +1) + min; System.out.println(randomNum); } public static void main(String[] args) { ranGen process = new ranGen(); if(process.random(null) > 50){ process.random50(null); } } }

最满意答案

看来,作业的目标是让你学习什么方法参数。 第一步是编写一个方法,返回1到100范围内的随机整数。因此,此方法没有任何参数。 它的签名应该是

public int generateRandomNumberBetween0And100()

第二步是编写一个方法,该方法采用指定顶部数字的参数。 因此,它的签名应该是:

public int generateRandomNumberBetween0And(int topValue)

第三步是编写一个方法,将两个值作为参数:bottom和top值。 因此,它的签名应该是:

public int generateRandomNumberBetween(int bottomValue, int topValue)

最后一步有点棘手。 它要求您记住方法已生成的值,并避免再次生成它们。 没有参数。 它的签名应该是

public int generateUniqueRandomNumber()

它的实现基本上应该做到以下几点:

1. generate random number 2. if random number is in the set of already generated numbers, go to 1 3. store the generated number in the set of already generated numbers 4. return the generated number.

当然,已经生成的数字集合在开头是空的。

请注意,您应该使用int而不是Integer 。 没有理由接受null作为参数,并且方法永远不应该返回null。 所以原始的int类型应该是首选。

另请注意,方法不应打印生成的数字,而是return它。 所以他们应该将int作为返回类型,而不是void 。

按照惯例,课程以大写字母开头,不要隐瞒单词。 所以你的类应命名为RandomGenerator 。

The goal of the homework, it seems, is to make you learn what method parameters are. The first step is to write a method that returns a random integer in the range of 1 to 100. So this method doesn't have any parameter. Its signature should be

public int generateRandomNumberBetween0And100()

The second step is to write a method which takes a parameter specifying the top number. Its signature should thus be:

public int generateRandomNumberBetween0And(int topValue)

The third step is to write a method taking two values as parameter: the bottom and the top value. Its signature should thus be:

public int generateRandomNumberBetween(int bottomValue, int topValue)

The last step is a bit more tricky. It asks you to remember the values a method has already generated, and to avoid generating them again. There is no parameter. Its signature should be

public int generateUniqueRandomNumber()

and its implementation should basically do the following:

1. generate random number 2. if random number is in the set of already generated numbers, go to 1 3. store the generated number in the set of already generated numbers 4. return the generated number.

Of course, the set of already generated numbers is empty at the beginning.

Note that you should use int rather than Integer. There's no reason to accept null as argument, and the methods should never return null. So the primitive int type should be preferred.

Also note that the methods should not print the generated number, but return it. So they should have int as their return type, and not void.

And classes, by convention, start with an upper-case letter and don't abbriviate words. So your class should be named RandomGenerator.

更多推荐