java input and random number games

Java

to understand java input

import java.util.Scanner ;

public class InDemo {
    public static void main (String[] args) {
        //Scanner input
        Scanner scanner = new Scanner(System.in ) ; String
        line = scanner.nextLine( ) ; System.out
         . println(line) ; line = scanner.nextLine() ; System.out .println (line) ; line = scanner.nextLine() ; System.out .println ( line) ; line = scanner.nextLine( ) ; System.out .println(line)
        
        
        
        
        
        ;
    }
}

This method cannot be universally applied, because it is not known how many lines

Therefore, scanner.hasNext() needs to be introduced to determine whether there is a next line

The improved code is as follows

import java.util.Scanner ;
public class InDemo {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in ) ; while
        ( true ) {
            boolean b = scanner.hasNextLine() ;
            if (! b){
                break;
            }
            String line = scanner.nextLine() ; System.out.println
            ( line ) ; }         System.out.println ( " end " ) ; }
        


    
}

// Computer's pseudo-random number
Random random1 = new Random( 100 ) ; System.out
.println ( random1.nextInt ( 100 )) ; System.out
.println ( random1.nextInt ( 100 )) ; System.out
.println ( random1.nextInt( 100 )) ;

The random numbers generated at this time seem to be random, but in fact the sequence is the same, and the result of each run is the same set of numbers, namely 15, 50, 74

// Computer's pseudo-random number
Random random1 = new Rand om () ;
System. out .println(random1.nextInt( 100 )) ;
System. out .println(random1.nextInt( 100 )) ;
System. out .println( random1.nextInt( 100 )) ;

If the number in Random() is removed, the random number generated at this time is determined by the current moment, and the time is always changing, so the random number generated each time is different, and it looks like it is really random.

With a basic understanding of the above knowledge, we can play a small game of guessing numbers.

import java.util.Random ;
import java.util.Scanner ;

public class guessnumber {
    // Guess the number , unlimited guesses [ 1-100 ] Only consider one game // steps     // 1 , generate 1 1-100 The random number is toGuess     // 2 , read a number input by the user as input     // 3 , greater than or equal to three cases public static void main (String[] args) {         Random random = new Random( 20211116 ) ; // Create a Random object and generate a pseudo-random number int
    
    


    

        toGuess = random.nextInt( 100 ) + 1 ; // Assign a random number from 1-100 to toGuess Scanner scanner = new Scanner(System.in ) ; // input while ( true ) { int input = scanner.nextInt() ; // Give the input integer number to input if (input < toGuess) { System.out                 .println ( " Guess is too small " ) ; } else if (input > toGuess)                 { System.out
        
        
            
            

            
.println( " guessed correctly " ) ;
            } else { System.out
                .println ( " guessed correctly " ) ;                 break;                       // guessed correctly, jump out of the loop }         }     } }


            


 

Try it yourself!

Related: java input and random number games