importjava.util.Random;importjava.util.Scanner;classPrintRandomNumbers{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);Randomgenerator=newRandom();System.out.println("How many random numbers?");intn=scanner.nextInt();System.out.println("What's the maximum number?");intmax=scanner.nextInt();for(inti=0;i<n;i++){intrandonNumber=generator.nextInt(max)+1;System.out.println(randonNumber);}}}
Guess a number
importjava.util.Random;importjava.util.Scanner;classGuessNumber{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);Randomgenerator=newRandom();inttoGuess=generator.nextInt(1000)+1;inttentatives=1;System.out.println("Guess a number:");intn=scanner.nextInt();while(n!=toGuess){if(n<toGuess){System.out.println("Wrong, it's too small");}else{// checking if n > toGuess is not strictly neededSystem.out.println("Wrong, it's too big");}System.out.println("Try again, guess a number:");n=scanner.nextInt();tentatives++;}System.out.println("Yeah, you've found it in "+tentatives+" tentatives!");}}
Pick a random number
importjava.util.Random;importjava.util.Scanner;classPickRandomNumber{publicstaticintrandom(inta,intb){Randomgenerator=newRandom();if(generator.nextBoolean()){returna;}else{returnb;}}publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("Give me a number:");intn1=scanner.nextInt();System.out.println("Give me another number:");intn2=scanner.nextInt();intrandomNumber=random(n1,n2);System.out.println("A random one of the two: "+randomNumber);}}
Pick a random element from array
importjava.util.ArrayList;importjava.util.Random;importjava.util.Scanner;classPickRandomElement{publicstaticStringrandom(ArrayList<String>values){Randomgenerator=newRandom();intrandomIndex=generator.nextInt(values.size());returnvalues.get(randomIndex);}publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("How many strings do you want to give me?");intn=scanner.nextInt();ArrayList<String>strings=newArrayList<>();System.out.println("Type "+n+" strings:");for(inti=0;i<n;i++){Strings=scanner.next();strings.add(s);}StringrandomString=random(strings);System.out.println("A random string of the ones you gave me: "+randomString);}}
Use dates
Which day of the week is today?
importjava.time.LocalDate;classDayOfWeek{publicstaticvoidmain(String[]args){LocalDatetoday=LocalDate.now();System.out.println("Today is "+today.getDayOfWeek());}}
Which day of the week is a date
importjava.time.LocalDate;importjava.util.Scanner;classDayOfWeek{publicstaticLocalDateaskDate(){Scannerscanner=newScanner(System.in);System.out.print("Day: ");intday=scanner.nextInt();System.out.print("Month: ");intmonth=scanner.nextInt();System.out.print("Year: ");intyear=scanner.nextInt();returnLocalDate.of(year,month,day);}publicstaticvoidmain(String[]args){LocalDatedate=askDate();System.out.println(date+" is "+date.getDayOfWeek());}}
Time passed between two dates
importjava.time.LocalDate;importjava.time.Period;classTimePassed{publicstaticvoidmain(String[]args){LocalDatemyBirthday=LocalDate.of(1985,4,25);LocalDatetoday=LocalDate.now();PeriodmyAge=myBirthday.until(today);System.out.println("Today I am "+myAge.getYears()+" years, "+myAge.getMonths()+" months and "+myAge.getDays()+" days old");}}
Random day of a year
importjava.time.LocalDate;importjava.util.Random;importjava.util.Scanner;classRandomDayOfYear{publicstaticLocalDaterandomDay(intyear){Randomgenerator=newRandom();// This is the 1st of January of the year given by the userLocalDatefirstDayOfYear=LocalDate.of(year,1,1);// Length of the year given by the user, in daysintlengthOfYear=firstDayOfYear.lengthOfYear();// A random number from 0 to 364 or 365intrandomN=generator.nextInt(lengthOfYear);// The random date is obtained by adding a random number of days to the 1st of JanuaryreturnfirstDayOfYear.plusDays(randomN);}publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("Of which year do you want a random date?");intyear=scanner.nextInt();LocalDaterandomDate=randomDay(year);System.out.println("A random date of that year is "+randomDate);}}