Scanner-based random number based Random

Reference variable defined data type and has a relatively fixed assignment step or format.

Data type  variable name  = new data type ();

 

Package guide: Import java.util.Scanner;

 

Create an object instance: Scanner Scanner new new SC = (the System.in);

 

Call the method:

 

I NT I = sc.nextInt (); for receiving the digital input console

 

String s = sc.next (); for receiving console input string

 

Complete understanding of Scanner class, we write code to use at it: ScannerDemo01.java

 

import  java.util.Scanner;

 

public class ScannerDemo01 {

 

public static void main(String[] args) {

 

// create Scanner reference type variable

 

Scanner sc = new Scanner(System.in);

 

// get digital

 

. System OUT .println ( " Please enter a number " );

 

int n = sc.nextInt();

 

. The System OUT .println ( "n- value "  + n-);

// get the string

. The System OUT .println ( " Please enter a string " );

String str = sc.next();

. The System OUT .println ( "STR value "  + STR);

}

}

 

Random number class Random

Random class, it can generate a variety of data types of the random number,

 

import  java.util.Random;

public class RandomDemo {

public static void main(String[] args) {

// Create a Random instance of the class

Random r = new Random();

// get 0-100 random integer in the range of randomly generated integer is assigned to the i variable

int i = r.nextInt(100);

// get 0.0-1.0 random decimal range, the decimal number is assigned to the generated random d variable

double d = r.nextDouble();

System.out.println(i);

System.out.println(d);

}

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/G-qz/p/11059645.html