Java Common Code

Source: Internet
Author: User

1. Store the date and time by the time of the millisecond
The JDK provides the System.currenttimemillis () method, which returns the number of milliseconds that are experienced at this time from 0 o'clock January 1, 1970.
Its data type is long, and this method is often used for timing operations.

2. Conditional Trinocular operator
A Boolean expression? Expression 1: Expression 2

3.switch-case and break joint use
Usually the case1, Case2 、... Casen correspond to a completely different operation and can be used in conjunction with the break statement.
Exit the switch block after executing the corresponding statement, and do not proceed with the following statement.

4. Using Beak statements in loops
Break can be used in loop statements or switch statements;
Break is used for looping, which causes the program to terminate the loop and execute the statements following the loop, which are often used with conditional statements.

5. Using continue statements in loops
Continue can only be used in the loop body
The function is to skip the remaining statements in the loop body and perform the next loop

6: Random number generation
int num= (int) (Math.random () *1000) +1;//(1-1000)

7. Random number generation
Random ran=new Random ()
Ran.nextint (1000);//(0-99)

8. User input
Scanner scan=new Scanner (system.in);
Scan.next.

9. Syntax for declaring arrays
data type [] array name =new data type [size];
Int[] arr and int arr[] Both can be
Array length is not specified when declaring an array, the new keyword allocates space to specify the amount of space allocated

10. Get the length of the array
Int[] Arr=new int[]{3,6,8,9};
int len=arr.length;

11.system.arraycopy method for array replication
public static arraycopy (Object src,int srcpos,object dest,int destpos,int length)

12.arrays.copyof method for array replication
Use the Copyof method of the Java.util.Arrays class to replicate the array
Type [] newarray=arrays.copyof (Type [] original,int newlength);
Feature: Generating a new array is a copy of the source array
Newlength Small source array, then intercept
Newlength is larger than the source array, it is populated with 0 or null

The 13.arrays.sort method is used to sort the array
The Arrays.sort () method provided by the JDK encapsulates the sorting algorithm of the array;
Arrays.sort (arr);

14. Bubble sort
System.out.println ("----------bubble sort Start----------");
for (int i=0;i<arr.length;i++) {
for (int j=0;j<arr.length-i-1;j++) {
if (Arr[j]>arr[j+1]) {
int T=ARR[J];
ARR[J]=ARR[J+1];
arr[j+1]=t;
}
}

15.return statements
Can be returned by a return statement, where the function of the return statement is to end the method and return the data to the calling method.

16. Guessing the alphabet game
public class Guessinggame {
public static void Main (string[] args) {
Indicates the number of times the player guesses
int count=0;
Represents the data that the user guesses
Char[] Input=null;
A string that represents a guess
Char[] Chs=null;
Used to save the results of the judgment
Int[] Result=new int[2];
Scanner scan=new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("guessinggame> Welcome to try to guess the alphabet game! ");
A string that represents a guess
Chs=generate ();
System.out.println ("Guessinggame> game begins, please enter the 5 alphabetical sequence you guessed: (exit--exit)");
while (true) {
String inputstr=scan.next (). Trim (). toUpperCase ();
if ("EXIT". Equals (Inputstr)) {
System.out.println ("guessinggame> Thank you for your attempt, goodbye!" ");
Break
}
Input=inputstr.tochararray ();
Result=check (Chs,input);
if (result[0]==chs.length) {
int score=100*chs.length-count*10;
System.out.println ("Guessinggame> Congratulations! Your score is: "+score);
Break
}else{
count++;
System.out.println ("guessinggame> you guessed to" +result[1]+ "characters, where" +
result[0]+ "characters are in the right place! (Total number = "+count+", exit--exit) ");
}
}
Scan.close ();
}
/**
* Randomly generate data that needs to be guessed
* @return storing an array of random characters
*/
public static char[] Generate () {
Char[] Chs=new char[5];
char[] letters={' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ',
' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ',
' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '};
Boolean[] Flags=new boolean[letters.length];
for (int i=0;i<chs.length;i++) {
int index;
bo=
index= (int) (Math.random () * (letters.length));
}while (Flags[index]);//Determine if the generated characters are duplicated
Chs[i]=letters[index];
Flags[index]=true;
}
return CHS;
}
/**
* Compare character sequences and sequences generated by the program, compare characters and their positions, and record comparison results
* @param CHS
* Program-generated character sequences
* @param input
* Character sequences entered by the player
* @return
* Store the results of the comparison. The length of the return value int array is 2, where
* The position of index 0 is used to hold the number of fully guessed letters (both characters and positions are correct),
* The position of index 1 is used to store the number of guessed pairs (the characters are correct, but the position is incorrect)
*/
public static int[] Check (char[] chs,char[] input) {
Int[] Result=new int[2];
for (int i=0;i<input.length;i++) {
for (int j=0;j<chs.length;j++) {
if (Input[i]==chs[j]) {//Determine if the character is correct
result[1]++;
if (i==j) {//determine location is correct
result[0]++;
}
Break
}
}
}
return result;
}
}

Java Common Code

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.