Java Basic syntax

Source: Internet
Author: User

The content of this section:

    • Reference data data type (Scanner, Random)
    • Process Control statements (if, for, while, Dowhile, break, continue)
Reference data type 1.1 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;

Create object instance: Scanner sc = new Scanner (system.in);

Call Method:

int i = Sc.nextint (); The number used to receive console input

String s = sc.next (); A string used to receive console input

After learning the scanner class, we write code to use it: Scannerdemo01.java

1 import Java.util.Scanner;2  Public classScannerDemo01 {3      Public Static voidMain (string[] args) {4         //Create a variable of the scanner reference type5Scanner sc =NewScanner (System.inch);6         //Get numbers7System. out. println ("Please enter a number");8         intn =sc.nextint ();9System. out. println ("the value of n is"+n);Ten         //Get String OneSystem. out. println ("Please enter a string"); AString str =Sc.next (); -System. out. println ("the value of STR is"+str); -     } the}

The result of the operation is as shown.

1.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.

    • Introduction to Methods

The public int nextint (int maxValue) produces a random integer in the range of [0,maxvalue], containing 0, which does not contain maxValue;

Public double nextdouble () produces a random fraction of 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:

    • How to use random:
    1. Import Guide Package: Owning package Java.util.Random
    2. Create instance format: Random variable name = new Random ();

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

1 import Java.util.Random;2 3  Public classRandomdemo {4      Public Static voidMain (string[] args) {5         //create an instance of the random class6Random r =NewRandom ();7         //get a random integer in the range of 0-100 and assign the resulting random integer to the i variable8         inti = R.nextint ( -); 9         //get the random decimals within the 0.0-1.0 range and assign the resulting random decimal number to the D variableTen         DoubleD =r.nextdouble (); OneSystem. out. println (i); ASystem. out. println (d); -}

The results of the operation are as follows:

Process statement 1. Marking

When a break statement appears in the inner loop of a nested loop, it can only jump out of the inner loop, and if you want to use the break statement to jump out of the outer loop, you need to add a tag to the external layer loop. The next step is to modify the Forfordemo.java, and the control program prints only 4 lines "*" as shown below. Breakdemo02.java

1  Public classBreakDemo02 {2      Public Static voidMain (string[] args) {3         intI, J;//define two loop variables4Itcast: for(i =1; I <=9; i++) {//Outer Loop5              for(j =1; J <= I; J + +) {//Inner Loop6                 if(I >4) {//determines whether the value of I is greater than 47                      BreakItcast;//Jump out of the outer loop8                 }9System. out. Print ("*");//Print *Ten             } OneSystem. out. Print ("\ n");//line Break A         } -     } -}

The result of the operation is as shown.

The tag "Itcast" is added before the outer for loop. When I>4, the break itcast is used, and the statement jumps out of the outer loop. So the program only prints 4 lines "*".

Java Basic syntax

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.