How to Write a Blurb With a Java Program

One thing a programmer can do with Java is present textual information to the user, either in a terminal or through a Graphical User Interface (GUI). Whichever way, the internal logic of the program is rather simple: create a list of strings to present ("blurbs") and set them to display randomly on a timer. In this way, you can cycle through the blurbs randomly, and keep the blurbs appearing on the screen.

Things You'll Need

  • Java Development Kit (JDK)
Show More

Instructions

    • 1

      Set up a Blurb class. This class will contain a String array, which will contain the blurbs, as well as a counter to count the number of blurbs:

      import java.io.*;
      import java.util.Random;

      class Blurb{

      String[] blurbs = new String[20];

      int count = 0;

      public static void main(String args[]){

      }
      }

    • 2

      Create a list of blurbs in an array:

      public static void main(String args[]){

      blurbs[0] = "blurb 1";
      blurbs[1] = "blurb 2";
      blurbs[2] = "blurb 3";
      blurbs[3] = "blurb 4";

      count = 4; //four blurbs

      }

    • 3

      Generate a random number in the main program:

      Random r = new Random(); //random number up to

    • 4

      Cycle through the blurbs at short intervals, using a random number each time:

      for (int i = count; i < count; i++){
      int current = r.nextInt(count); //random number between 0 and 3
      System.out.println(blurbs[current]);

      Thread.sleep(10000); //pause for ten seconds
      }

Related Searches:

References

Comments

Related Ads

Featured