• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code not working

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
}

 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, when posting code please UseCodeTags (←click that link to learn how).
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79514
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic