Use of Java ArrayList Random Scanner

java2


Constructor method is designed to create an object, when we create an object by keyword new, in fact, call the constructor method

format:

public class name (parameter type parameter name) {
Method member
}

Notes:
1. The name of the constructor must be exactly the same as the name of the class, and even have the same capitalization
2. Do not write constructor return type, not even void write
3. constructors can not return a specific return value
4. If you do not write any constructors, the compiler will default presented a constructor, with no parameters, the method body do nothing
5. Once you've written a constructor method, the compiler will not be presented


A standard class usually have the following four components
1. Therefore, member variables must be modified using the private key
2 for each member variable write one pair Getter / Setter method
3. Preparation of a no-argument constructor
4. write a full-constructor argument

Note that the standard class, also known as java Bean

================================================== ================================================== ====
Scanner class function, keyboard input data to the program which


// class Scanner function, keyboard input data, into which the program
// reference type generally used step:
@ 1 leader packet.
@ 2 create a class name of the object class name name = new ().
// 3. the method of using the object name members.
Import java.util.Scanner;
public class Demo01Scanner {
public static void main (String [] args) {
// Create 2. 1
//System.in representative input from the keyboard
Scanner sc = new Scanner ( System.in);

// int 3 acquires the numeric keypad to enter
System.out.println ( "Enter number:");
int NUM = sc.nextInt ();
System.out.println ( "int digital input is:" + num);

// Get keyboard input string
System.out.println ( "input character string");
String = sc.next STR ();
System.out.println ( "is the input string:" + STR);
}
}

=========================================================================================================================================

// Example 2

java.util.Scanner Import;
// calculated and the two input digital
public class Demo02Scanner {

static void main public (String [] args) {
Scanner Scanner new new SC = (the System.in);
System.out.println ( "Enter first number:");
int sc.nextInt A = ();
the System. out.println ( "Please enter the second number:");
int sc.nextInt B = ();

RES = A + B int;
System.out.println ( "The result is:" + RES);
}
}

==================================================================================================================================================

// example. 3
Import java.util.Scanner;
// calculate the maximum value of a three
public class Demo03ScannerMax {
public static void main (String [] args) {
System.out.println ( "Please enter a value:" );
Scanner Scanner new new SC = (the System.in);
int sc.nextInt A = ();
System.out.println ( "Please enter the second value:");
int sc.nextInt B = ();
the System. out.println ( "Please enter the third value:");
int sc.nextInt C = ();

// first the first two digits give the maximum value among
int TEMP = A> B A: B;?
Int max = TEMP> C TEMP:? C;
System.out.println ( "maximum is:" + max);
}
}

=====================================================================================================================================================


Anonymous object is the only object on the right, left with no name and assignment operator

mew class name ();
Note: The only anonymous object can only be used once, and then not the next no longer create a new object, the object only if it is determined there is a need to use only once, you can use
anonymous objects;,;


==============================================================================================================================================================================


Random use

java.util.Random import;

{class Demo01Randow public
public static void main (String [] args) {
// class is used to generate random numbers Randow

= R & lt new new the Random the Random ();
int NUM = r.nextInt ();
System.out.println ( "random number is:" + NUM);
}
}
=============== ================================================== ===================

 

java.util.Random import;

public class Demo02Random {

static void main public (String [] args) {
// use Randow numbers within the specified range to generate
the Random R & lt new new = the Random ();
// generates a hundred times
for (int I = 0; I <100; I ++) {
int num = r.nextInt (10); // actually range from 0 to 10
System.out.println (NUM);
}
}
}


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

Demo04RandowGame class {public
@ small game viewing
@ 1 needs to generate a first random number, and does not change once generated, by nexInt method of Randow
// need keyboard input, it is used Sanner
// Get the keypad input with among nexInt Scanner () method
// two numbers has been determined

public static void main(String[] args) {
Random r = new Random();

int randomNum = r.nextInt(100) + 1;

Scanner sc = new new Scanner (System.in);
System.out.println ( "Please enter the number you guess:");

sc.nextInt guessNum = int ();
IF (guessNum> randomNum) {
System.out.println ( "too big");
} the else IF (guessNum <randomNum) {
System.out.println ( "too small") ;
} the else {
System.out.println ( "guessed");
}
System.out.println ( "gAME OVER");
}
}

 

====================================================================================================================================

ArrayList of use

1. The length of the array is not altered
2. ArrayList but the length can be set arbitrarily changed;

 

import java.util.ArrayList;

// Note:
// for ArrayList, the direct printing not get an address value, but the content
// if the content is empty, the resulting empty List
public class Demo01ArrayList {
length // array may not occur change
// ArrayList but the length is free to change the set of
public static void main (String [] args) {
// for ArrayList, there is a Control number <E> Representative generic.
// generic, that is, all the elements contained in the collection among all types are unified
// note that generics can only be a reference type, not a basic type


// Create an ArrayList collection, name of the collection is a list, which is filled with all types of String

ArrayList<String> list = new ArrayList<>();

// add some data to the collection, you need to use the add method.
List.add ( "HHHHHH");
System.out.println (List);


// ArrayList among the commonly used methods are:
//1.add (), add elements to the collection, the return value is successful
list.add ( "Zhao Liying");
list.add ( "Andy");
list.add ( " Chao ");
//2.get (), obtained from the collection of data, the parameter is the index
String name = List.get (2);
// 3.remove delete data set, the parameter is the index value, the return delete data
String = Re list.remove (2);
() Get the length set //4.Size;
int list.size S = ();

 

// If desired set ArrayList which stores basic data types, must be substantially corresponding to the type "packaging";
// basic types of packaging (reference type, are located under java.Lang packaging bag),
// byte Byte
@ Short Short
// int Integre
// Long Long
// float float
// char Character
// boolead Boolean


// Starting Jdk 1.5 autoboxing support, automatic unpacking
@ autoboxing: Basic Type == "Package Type
// automatically unpacking: Packaging Type ==" base type
}
}

 

Guess you like

Origin www.cnblogs.com/Alvatang/p/11680788.html