Results 1 to 4 of 4
- 01-23-2012, 09:36 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
writing a program to choose a sentence and random stuck
Using java.util.Random to choose a random sentence and print it
Hi everyone,
First post here! To preface, I am extremely new to Java and programming in general. The extent of my work with the program is simple JOptionPane's and simple math problems and the use of the Scanner to plug numbers into equations.
Currently I am trying to write a simple program that selects a String sentence from a list of 4 sentences at random and print it. I am stuck as to how to do this. Here is my syntax so far:
Code:
--------------------------------------------------------------------------------
Java Code:import java.util.Random; public class Sentences { public static void main(String[] args) { Random sentences = new Random(); String sent1 = "This is a test!"; String sent2 = "This is a test quote!"; String sent3 = "This is a test quote again!"; String sent4 = "This is a test quote again again!"; System.out.println (); } }Last edited by Eranga; 01-24-2012 at 07:56 AM. Reason: code tags added
- 01-23-2012, 10:55 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: writing a program to choose a sentence and random stuck
Dont use 4 single string variables, use a string array, so you can generate with help of the random class a integer from 0 to 3. 0 = array[0] = string1 !
- 01-24-2012, 07:55 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: writing a program to choose a sentence and random stuck
Keep your sentences in an array (or any other collect. forget about is if you don't know about it). Pick the random number within the valid range (which is through the number of sentences you have) and pick the sentence and display. In that way you know need to stick to four sentences.
And also please use code tags when you are posting code segments next time. Sometimes unformatted codes are difficult to read.
- 01-24-2012, 08:29 AM #4
Member
- Join Date
- Aug 2011
- Location
- INDIA
- Posts
- 64
- Rep Power
- 0
Similar Threads
-
Using java.util.Random to choose a random sentence and print it
By skylerrivera17 in forum New To JavaReplies: 0Last Post: 01-23-2012, 09:12 AM -
stuck on writing Date application
By gabrielpr12 in forum New To JavaReplies: 2Last Post: 01-07-2012, 08:31 PM -
Random sentence generator
By BrAD19924 in forum New To JavaReplies: 6Last Post: 05-03-2011, 02:58 AM -
How can I let someone choose the name of a file for my program to read?
By falsealarm in forum New To JavaReplies: 2Last Post: 04-03-2009, 04:37 AM -
Program where the user can choose the algorithm
By le_albina@hotmail.com in forum New To JavaReplies: 1Last Post: 03-18-2009, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote.gif)

Bookmarks