• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code not working  RSS feed

 
Jitin Reddy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class island{
public static void main(String[] args){
int[] index=new int[4];
index[0]=1;
index[1]=2;
index[3]=0;
index[2]=3;
String[] islands=new String[4];
islands[0]="Bermuda";
islands[1]="Fiji";
islands[2]="Hawaii";
islands[3]="Andaman";
int y=0;
while(y<4){
int ref=(int)(math.random()*4);
System.out.print("Island=");
System.out.println(islands[ref]);
y=y+1;
}
}
}

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TellTheDetails

Does it compile? If not, what is the compiler error?

Does it run correctly? If not, what happens? And what is supposed to happen?


Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when posting code please UseCodeTags (←click that link to learn how).
 
ab singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i Think you have written wrong syntax for math function
as java is case sensitive so just replace


with

and also import the package
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would have added code tags to your post, but you didn't indent the code.
There is no need to import the Math class. I advise against using that sort of code. Never mind what it say sin the Java Tutorials about Math#random being more convenient, you are going to get a much simpler bit of code with
Random.nextInt(4);
You do need to import the Random class.

By the way: tell me what you think the largest possible result of (int)(Math.random() * 4) and Random.nextInt(4) would be. Will it be the same possible maximum in each case? Look at this old discussion about the Random class and random method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!