hi
below code is an example.I hav couple of questions
1)The highlighted bold red code(myParrot[0]...)can it be simplifyed?
2)Can i use random to generate a given set of numbers?How?

public class Bird{
public static void main(String[] args) {

		
		int[] myParrot = new int[3];
		[B]myParrot[0]=1;
		myParrot[1]=2;
		myParrot[2]=3;[/B]
		
		for (int feature=0; i<myParrot.length; feature++) {
			System.out.print(myParrot[feature]);
			System.out.print(" ");
			}
		System.out.println();
			}
		}

int[] myParrot = {1,2,3}; // simpler

You can use Random to generate random numbers. I donlt kniw what you mean by "a given set of numbers"

ok i came up with the below code.Another question
1)Anyway of prevent the random number from displaying zeros?

public class Bird{
public static void main(String[] args) {

		
		int[] myParrot = new int[3];
		for (int i=0;i<3;i++){
		myParrot[i]=(int)(Math.random()*3);
		}
		
		
		for (int i=0; i<myParrot.length; i++) {
			System.out.print(myParrot[i]);
			System.out.print(" ");
			}
		System.out.println();
			}
		}

Edited 4 Years Ago by Valiantangel: n/a

Ok here is the code

public class Bird{
public static void main(String[] args) {


int[] myParrot = new int[3];
for (int i=0;i<3;i++){
myParrot=(int)(Math.random()*3);
}


for (int i=0; i<myParrot.length; i++) {
System.out.print(myParrot);
System.out.print(" ");
}
System.out.println();
}
}

Could you show the code that solved the problem?

Did you test it well?

Edited 4 Years Ago by NormR1: n/a

Thanks zeroliken, i will hav a look
NormR1 below is the code.
Also i got 1 question.
1.What if i like to change the random output to hav a negative number, like -1?

public class Bird{
public static void main(String[] args) {


int[] myParrot = new int[3];
for (int i=0;i<3;i++){
myParrot[i]=(int)(Math.random()*3)+1;
}


for (int i=0; i<myParrot.length; i++) {
System.out.print(myParrot[i]);
System.out.print(" ");
}
System.out.println();
}
}

What is the range of numbers you want?
If you want to skip over 0 then you will have to write special code to select a number from two ranges.

no i dont want any range.just wondering if the negative sign can mirror the above positive output.
Lets say the above ouput gave 12345 can i hav a code to display a negative output -1-2-3-4-5 ?

The random() method returns a number in a small range. You can change its range by adding to it and by multiplying it by some numbers.

Edited 4 Years Ago by NormR1: n/a

This question has already been answered. Start a new discussion instead.