+ 1
Java
In Java, How to generate random numbers and nouns changing each time we run? Also... I didn't quite understand Scanner How is it different from BufferedReader; when and how to use it? (Can anyone please just make a small simple program using Scanner for my better understanding...)
4 odpowiedzi
+ 16
1) Generating Random number
//using Random Class
● import Random class
//import java.util.Random;
●create an object of that class & generate a random number from integer a to b
/*
Random R1 = new Random();
number1 = a + R1.nextInt(b) + 1 ;
//a+1 is added because R1.nextInt(b) generates random number from 0 to (b-1).
//edited Michal ☺ (thnx for noticing)
*/
2) Using Scanner Class to take input
● import java.util.Scanner; //importing Scanner class
● Scanner sc = new Scanner(System.in); //making object of Scanner class
● int number = sc.nextInt(); //for taking Integer input
//U can have a look at more input methods for different data types here : https://www.sololearn.com/learn/Java/2220/
3) Follow this link for proper differences between Scanner & Buffer Class in java : https://www.google.com/amp/s/www.geeksforgeeks.org/difference-between-scanner-and-bufferreader-class-in-java/amp/
+ 3
Pro Coder
You can get random city for example that way
https://code.sololearn.com/cOoW0Jmo7SPU/?ref=app
//
Gaurav Agrawal, actually nextInt(b) generates from 0 to b-1
+ 2
Gaurav Agrawal Thanks
Michal Thanks
+ 1
Gaurav Agrawal Do you know how to print random names(city names etc.)?