Advertisements

Write a program that takes two int values a and b from the command line and prints a random integer between a and b.

import java.lang.Math;
import java.util.Scanner;

public class RandomNumbers{

public static void main(String args[])
{
System.out.println(“Enter a number “); //This prompts the user to enter a number
Scanner scan = new Scanner(System.in);
double t = scan.nextDouble(); //Stores the valuse as a double

System.out.println(“Enter a second number “); //This prompts the user to enter a 2nd number
double v = scan.nextDouble(); //Stores the valuse as a double

double w = Math.floor(Math.random()*(t-v+1)+v); //assigns w to a random number in between t & v
System.out.println(w); //prints w

}
}

Advertisements

About Rant4u

A Revolution in Social Networking

Posted on February 18, 2014, in Java programming and tagged , , , , . Bookmark the permalink. Leave a comment.

Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: