Arrays in a do...while
I have an assignment that will compile, but I need to put an array that will display 6 numbers instead of one. It has to be do..while and I need an array. I am not asking for the solution, just a hint in where code for the array should go. If I am on the right track. Here's what I have:
// bz.java
//
import java.util.Random;
public class bz
{
public static void main( String args[] )
{
Random randomNumbers = new Random();
int startRandomNumbers= 1;
int maxRandomNumber = 56;
do
{
int chooseRandomNumber = startRandomNumbers + randomNumbers.nextInt(maxRandomNumber);
System.out.printf("\n%s%d\n\n","The random number is ",chooseRandomNumber);
}
while (1 >= 56);
} //end main
} //end class
You realize that a do-while is meant to do loops, but
while (1 >= 56);
, there's no point in using the do-while, since that will always be false... use variables, for example
do
print something, increase number of printings by 1
while number of printings < 6
Well, yeah, but I find it hard to believe that the assignment just said "hey, just throw an array in there randomly somehow. Cowabunga dude!"
Keep in mind that homework assignments often ask for very specific implementations, or make arbitrary and often bizarre restrictions that you wouldn't do in real life.
Just quote the actual assignment.
Verbatim:Write a program, that uses a do loop, then create six random numbers, from 1 to 56. Load these 6 numbers into a six-element array. Write a do loop to display all of the elements of the array.
So you need two do/while loops. First to generate the random numbers and store them in an array. Second one to display the numbers.
declare an array of length 6
loop
generate a random number and store in array
end loop
loop
print out number from array
end loop
> he design part is which satisfy me the most
Yet this is where most people fail. They are given an assignment and their initial response is "I don't know how to do that". When I was tutoring I often heard students complain how hard Java was. Wrong! It is their inabilty to figure out the algorithm that is their failing. They already know and understand the syntax. This is why 9 times out of 10 the first response people get is "How would you do it on paper?"