The Artima Developer Community
Sponsored Link •

Design Forum
String Arrays in Java

3 replies on 1 page. Most recent reply: Jan 13, 2003 6:15 PM by Ugo Posada

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Kelly Ferris

Posts: 2
Nickname: javagrl
Registered: Oct, 2002

String Arrays in Java Posted: Oct 18, 2002 3:37 PM
Reply to this message Reply
Advertisement
Any suggestions for this program?
Randomly generating a result like in the game of clue. The murderer, weapon and room, and displaying all three results in one line.
I know you would use a String array, just not sure how to do it exactly.
Thanks in advance!!


Parag Doshi

Posts: 2
Nickname: parag
Registered: Oct, 2002

Re: String Arrays in Java Posted: Oct 22, 2002 9:00 AM
Reply to this message Reply
Can you elaborate on your requirement please?

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: String Arrays in Java Posted: Jan 13, 2003 6:10 PM
Reply to this message Reply
Here you go, hope it helps.

import java.util.Random;
 
public class Clue {
	private String[] murderers = {"French maid", "Butler", "Inspector Gadget", "Bill Gates"};
	private String[] weapons = {"Short skirt", "Mayonaise", "Gadget", "Palladium"};
	private String[] rooms = {"Motel room", "Computer lab", "Pool", "Microsoft headquarters"};
	private Random myRandom;
	
	public Clue() {
		myRandom = new Random();	    
	}
	
	public String getMurderer() {
	    int myRandomInt = myRandom.nextInt(murderers.length);
	    return murderers[myRandomInt]; 	
	}
	
	public String getWeapon() {
	    int myRandomInt = myRandom.nextInt(weapons.length);
	    return weapons[myRandomInt];     	
	}
	
	public String getRoom() {
		int myRandomInt = myRandom.nextInt(rooms.length);
	    return rooms[myRandomInt]; 		
	}
	
	public static void main(String[] args) {
	    Clue myClue = new Clue();
	    //Loop just to check the randomness of the solution
	    for (int i = 0; i < 5; i++) {
		    System.out.println("The murderer is: "+ myClue.getMurderer());	
		    System.out.println("The weapon is: "+ myClue.getWeapon());
		    System.out.println("The room is: "+ myClue.getRoom());
	    }
	}			 		
}
 

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: String Arrays in Java Posted: Jan 13, 2003 6:15 PM
Reply to this message Reply
Didn't read the "in one line" stuff, the only change would be:

public static void main(String[] args) {
 Clue myClue = new Clue();
 //Loop just to check the randomness of the solution
 for (int i = 0; i < 5; i++) {
    System.out.println(myClue.getMurderer()+ " "+    myClue.getWeapon() + " " + myClue.getRoom());
 }
}

Flat View: This topic has 3 replies on 1 page
Topic: JavaMail Previous Topic   Next Topic Topic: Case study : XML Messaging

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use