Reference type scanner classes and random types in Java

Source: Internet
Author: User

Scanner class

We're going to learn that the scanner class belongs to the reference data type, and we first understand the reference data type.

Use of reference data types

Unlike defining basic data type variables, variable definitions and assignments of reference data types have a relatively fixed step or format.

Data type  variable name  =  new data type ();

Each reference data type has its function, and we can invoke the functionality of the instance of that type.

Variable name. Method name ();

Scanner class

Scanner class is a kind of reference data type, we can use this class to complete the user keyboard input, access to input data.

Scanner Steps to use:

// Guide Package: Import Java.util.Scanner; // To create an object instance: New Scanner (system.in); // Calling Methods int // the number used to receive console input // a string used to receive console input

Knowing the scanner class, we write code to use it:

Scannerdemo01.java

ImportJava.util.Scanner; Public classScannerDemo01 { Public Static voidMain (string[] args) {//Create a variable of the scanner reference typeScanner SC=NewScanner (system.in); //Get numbersSystem.out.println ("Please enter a number"); intn =Sc.nextint (); System.out.println ("The value of n is" +N); //Get StringSystem.out.println ("Please enter a string"); String Str=Sc.next (); System.out.println ("The value of STR is" +str); }}

The result of the operation is as shown.

2. Random number Classes

Let's learn the class random, which is used to generate random numbers, which also belongs to the reference data type.

In this random class, it can produce random numbers of various data types, here we mainly describe the way to generate integers and decimals.

 Public int nextint (int maxValue)    //  generates [0,maxvalue] a random integer in the range, containing 0, which does not contain maxValue;  publicDouble nextdouble ()  // produces a random decimal number in the range of [0,1], which contains 0.0 and does not contain 1.0. 

The use of reference data types, when learning keyboard input scanner, we have already learned, here, again review:

L How to use random:

N Import Guide Package: Owning package Java.util.Random

n Create instance format: Random variable name = new Random ();

Next, learn the use of the random class with a piece of code.

Randomdemo.java

ImportJava.util.Random;
Public classRandomdemo { Public Static voidMain (string[] args) {//create an instance of the random classRandom R=NewRandom (); //get a random integer in the range of 0-100 and assign the resulting random integer to the i variable inti = r.nextint (100); //get the random decimals within the 0.0-1.0 range and assign the resulting random decimal number to the D variable DoubleD =r.nextdouble (); System.out.println (i); System.out.println (d); }}

The results of the operation are as follows:

Guess number Case 1.1 case Introduction

In our daily life, it is very interesting to play a number game with our friends. Now let's write this guessing game out of Java.

Guess what the number of cases is to complete what kind of function? As the name implies, this game is you out of a number, I guess.

The game operates as follows:

L Background Pre-generate a random number between 1-100, user keyboard input guess number

L If you guessed it, print "Congratulations, right."

L if wrong: guess big: print "Sorry, you guessed big!" Guess small: print "Sorry, you guessed small!"

Until the number is guessed.

1.2 Case needs analysis,

L 1. Generate a random number between 1-100 by using the method Nextint () in the Random class

L 2. Enter the guessed number

L 3. To guess the wrong number by a while loop

N Guess right, jump out of the loop, game over

N guess wrong, according to the results, give hints, and then guess the numbers, the game continues

If you guessed big, print sorry, you guessed big! Continue to the next cycle

U if the guess is small, print sorry, you guess small! Continue to the next cycle

1.3 Implementing Code Steps

After the analysis is complete, in the main () method, we work together to write the code: Guessnumber.java

 Public classGuessnumber { Public Static voidMain (string[] args) {//1. Generate a random number between 1-100 by using method Nextint () in the Random class        intRandomnumber =NewRandom (). Nextint (100); System.out.println ("Random number has been generated!" "); //2. Enter a number to guessSystem.out.println ("----Please enter the number you guessed:----"); Scanner SC=NewScanner (system.in); intEnternumber =Sc.nextint (); //3. Pass the while loop to guess the wrong number//guess right, jump out of the loop, game over         while(Enternumber! =Randomnumber) {            //guess wrong, according to the results, give hints, and then guess the numbers, the game continues            if(enternumber>Randomnumber) {                //If you guessed big, print sorry, you guessed big! Continue to the next cycleSystem.out.println ("Sorry, you guessed it!" continued the next cycle.); }Else {                //If the guess is small, print sorry, you guess small! Continue to the next cycleSystem.out.println ("Sorry, you guessed small!" continue to the next cycle.); }            //Enter a number to guessSystem.out.println ("----Please enter the number you guessed:----"); Enternumber=Sc.nextint (); } System.out.println ("Congratulations, that's correct!" "); }}

Reference type scanner classes and random types in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.