i think i have my logic wrong
i have 2 arrays that generate random cities and post the cities to my applet...
this is all working fine..
but sometimes the applet will randomly generate two of the same cities
like: Miami Miami
here is my code
and i know my logic is wrong in my if loop
or maybe i dont need that index and index1 for the same array???
any suggestions
thanks in advance annie
Code:import java.awt.*; import javax.swing.*; import java.util.*; public class myAppletTest extends JApplet { private String arrLocal[] = new String[5]; private String arrForeign[] = new String[3]; Random rnd = new Random(); int index = rnd.nextInt( arrLocal.length ); String randomLocal = arrLocal[index]; Random rnd1 = new Random(); int index1 = rnd.nextInt( arrLocal.length ); String randomLocal1 = arrLocal[index1]; Random rnd2 = new Random(); int index2 = rnd.nextInt( arrForeign.length ); String randomForeign = arrForeign[index2]; public void init() { arrLocal = new String[] { "New York", "Boston", "Miami", "Philadelphia", "Chicago" }; arrForeign = new String[] {"Madrid", "London", "Paris"}; for (int k = 0; k < arrLocal.length; k ++) System.out.println(arrLocal[k]); for (int i = 0; i < arrForeign.length; i ++) System.out.println(arrForeign[i]); if(arrLocal[index]==arrLocal[index1]) { for (int k = 0; k < arrLocal.length; k ++) System.out.println(arrLocal[k]); } }//end init() public void paint(Graphics g) { g.drawString( "We have special rates to: " , 5 , 30); g.drawString( arrLocal[index] , 5 , 45); g.drawString( arrLocal[index1] , 5 , 60); g.drawString( "Inexpenive vacations to: " , 5 , 90); g.drawString( arrForeign[index2] , 5 , 105); } }




Reply With Quote