GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java-Programs/Basic-Java/RandomNumberGeneration.java at master · iamarav/Java-Programs Expand file tree/ Copy pathRandomNumberGeneration.javaMore file actions 83 lines (64 loc) · 1.87 KB / Copy pathRandomNumberGeneration.javaFile metadata and controls83 lines (64 loc) · 1.87 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 import java.util.Scanner; public class RandomNumberGeneration { public static void main(String[] args) { System.out.println("Program to generate a Random Number\n---"); int option = 0; Scanner sm = new Scanner(System.in); System.out.print("Enter 0 to generate any random number OR " + "\nEnter 1 to generate a random number between a range" + "\nEnter 2 to generate a random of fixed length: "); // DEFINED SEPARATELY FOR BETTER UNDERSTANDING option = sm.nextInt(); if (option ==0) { generateRandom(); } else if (option ==1) { generateRandomBetweenRange(); } else if (option ==2) { generateRandomWithLength(); } else { System.out.println("Invalid Option. Try Again"); end(); } sm.close(); } private static void generateRandom() { int a = (int) ((Math.random()*((10000-0)+1))+0); System.out.println("OUTPUT: "+a); end(); } private static void generateRandomBetweenRange() { Scanner ss = new Scanner(System.in); System.out.print("Enter lower limit: "); int lower = ss.nextInt(); System.out.print("Enter upper limit: "); int upper = ss.nextInt(); ss.close(); if (upper<lower) { System.out.println("Upper cannot be lesser than lower"); //RECOMMENDED end(); } if(upper<0 ||lower<0) { System.out.println("Limits cannot be lesser than zero"); //RECOMMENDED end(); } int a = (int) ((Math.random()*((upper-lower)+1))+lower); System.out.println("OUTPUT: "+a); end(); } private static void generateRandomWithLength() { Scanner ss = new Scanner(System.in); System.out.print("Enter the length (int max =10): "); int len = ss.nextInt(); int a = (int) (Math.random()*(Math.pow(10, len))); System.out.println("OUTPUT: "+a); ss.close(); end(); } private static void end() { System.out.println("---\nThe Program has ended."); System.exit(0); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |