Third day of the Java Foundation

Source: Internet
Author: User
Tags arithmetic

# # #01创建引用类型变量公式
* A: Create A reference type variable formula
* A: We want to learn the scanner class is a reference data type, we first understand the reference data type.
* B: The definition format of the reference data type
* 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 ();
* C: Use of reference data types
* Each reference data type has its function, and we can invoke the functionality of the instance of that type.
* Variable name. Method name ();


# # #02Scanner类的使用
* Use of the A:scanner class
* A: Guide package import Java.util.Scanner;
* B: Create keyboard Entry Object Scanner sc = new Scanner (system.in);
* C: Reads an integer from the keyboard entry
* int enternumber = Sc.nextint ();
* D: Read the keyboard input string
* String enterstring = Sc.next ();
* B: Case code
Import Java.util.Scanner;
public class demo05scanner{
public static void Main (string[] args)
{
Scanner sc = new Scanner (system.in);

int enternumber = Sc.nextint ();
System.out.println ("The integer entered by the user is" +enternumber);

String enterstring = Sc.next ();
SYSTEM.OUT.PRINTLN ("User input string is" +enterstring);
}
}


# # #03Random随机数类的使用_1
* The use of A:random random number classes _1
* A: function
* Generate random numbers need to use to reference type random number class
* B: How to use
* Import Guide Package: belongs to package java.util. Random
* Create Instance format: Random random = new random ();
* Call method
* Nextint (int maxValue) produces a random number in the [0,maxvalue] range, containing 0 not containing maxValue
* Nextdouble () generates a [0,1] range of random numbers
Such as:
Random random = new random ();
int mynumber = random.nextint (100);//The result is a number of 0-99
* B: Case code
Import Java.util.Random;
public class randomdemo{
public static void Main (string[] args) {
Random ran = new random ();
The function of generating random numbers in the random class
int i = ran.nextint (100);
System.out.println (i);

Problem? Generate random number, range 1-100
Nextint (100) 0-99 + 1
}
}

# # #04Random随机数类的使用_2
* The use of A:random random number classes _2
* A: Call method
* Nextdouble () generates a [0,1] range of random numbers
Such as:
Random random = new random ();
int mynumber = random.nextdouble ();//The result is a number between 0.0-1.0 (including 0.0 not including 1.0)

# # #05if语句格式第一种
* A:IF statement format the first type
* A: Writing format
if (comparison expression) {
Statement body;
}
* B: Execution process:
* The value of the comparison expression is evaluated first to see if its return value is true or false.
* If true, executes the statement body;
* If False, the statement body is not executed;
* B: Case code
public class ifdemo{
public static void Main (string[] args) {
int i = 5;
If judgment on the variable i
if (i > 5) {
System.out.println ("If the condition is true");
i++;
}

System.out.println (i);
}
}

# # #06if语句格式第二种
* A:IF Statement format Second
* A: Write format
if (comparison expression) {
Statement body 1;
} else {
Statement body 2;
}
* B: Execution process:
* The value of the comparison expression is evaluated first to see if the return value is true or false.
* If true, executes the statement body 1;
* If False, executes the statement body 2;
* B: Case code
public class ifelsedemo{
public static void main ( String[] args {
int i = +;
//Determine the variable, which is the odd even, divided by 2, to see if the remainder is 0 or 1
if (i% 2 = = 0) {
System.out.println (i+ "is an even number");
}else{
System.out.println (i+ "is odd");
}
}
}


# # #07if语句格式第三种
* A:IF statement format Third
* A: Writing format
if (comparison expression 1) {
Statement body 1;
} else if (comparison expression 2) {
Statement body 2;
} else if (comparison expression 3) {
Statement body 3;
}
...
Else {
Statement body n+1;
}
* B: Execution process:
* First evaluates the comparison expression 1 to see if its return value is True or FALSE,
* If true, executes the statement body 1,IF statement end.
* If False, then evaluates the comparison expression 2 to see if its return value is True or FALSE,

* If true, executes the statement body 2,IF statement end.
* If False, then evaluates the comparison expression 3 to see if the return value is True or FALSE,

* If all is false, the statement body n+1 is executed.
* B: Case code
public class ifelseifdemo{
public static void Main (string[] args) {
//score judgment required, score >80 Achievement >70 score & GT;60
//define variable, save score
int grade =;
Use the If else if statement to determine the results of
if (Grade >) {
System.out.println (grade+ "score is excellent");
} else if (Grade >) {
System.out.println (grade+ "score is good");
} else if (Grade >) {
System.out.println (grade+ "score is Medium");
} else{
System.out.println (grade+ "score is poor");
}

}
}

# # #08if语句和三元运算符的互换
* A: Ternary operator
* A: Concept
* Used to complete simple selection logic, i.e. according to the criteria, choose one of the two choices to perform
* B: Use format
* (conditional expression)? Expression 1: Expression 2;
* C: Arithmetic rules
* 1: Judging the conditional expression, the result is a Boolean value
* 2:true, the result of the operation is an expression 1
* 3:false, the result of the operation is an expression 2
* B: Case code
public class ifelsedemo_1{
public static void Main (string[] args) {
int j = 6;
int i = 15;
Use the IF statement to determine the maximum value
if (i>j) {
int j = 6;
System.out.println (i+ "is the maximum value");
}else{
System.out.println (j+ "is the maximum value");
}

Using ternary arithmetic to implement
int k = i>j? I:j;
System.out.println (k + "is the maximum value");
}
}
* C: Using an If statement or ternary expression
* More criteria to judge, use if
* Ternary, must have results, if can have no result


# # #09while循环
* A:while Cycle structure
* A: Use format
Initialize the expression;
while (condition) {
Loop body
}
* B: Execution order
When the condition is true, the loop body is executed and after the loop body is executed
The program executes the condition in the while and, if the condition is true, resumes execution of the loop body
Until the condition is false, the loop ends.
* B: Case code
public class whiledemo{
public static void Main (string[] args) {
An integer between output 1-4
Define variables, integer types, cyclic conditions
int i = 1;
while (I < 5) {
System.out.println (i);
i++;
}
}
}


# # #10for循环_1
* A:for Cycle _1
* A: Use format
for (initialize variable; condition; increment) {
Circulation body;
}
* B: Explanation of each module
Initialize variables: Define variables that are used to control the number of loops
Condition: When the condition is true, the loop body is executed, and the condition is false, ending the loop
Increment: variable self-increment condition
* B: Case code
public class fordemo{
public static void Main (string[] args) {
For loop, output 0-10
for (int i = 0; i < i++) {
System.out.println (i);
}
}
}

# # #11for循环_2
* A:for Loop Execution Flow
for (①; ②; ③) {

}
The first step is to execute ①
Step two, execute ②, if the result is true, perform step fifth if the result is false
Step three, execute ④
Fourth step, execute the ③, then repeat the second step
Fifth step, exit the loop

# # #12for循环_3
* A: Case
* A: Calculate 1+4 results with a For loop
* B: Case code
public class fordemo_1{
public static void Main (string[] args) {
Define variables, record the sum of the data
int sum = 0;
Use loops to change variables from 1 to 4
for (int i = 1; I <= 4; i++) {
To sum a variable
sum = sum + i;
}
SYSTEM.OUT.PRINTLN (sum);
}
}

# # #13do_while循环
* A:do_while Cycle
* A: Use format
do{
Circulation body;
}while (conditions);
* B: Execution order
The loop body is executed first, then the condition is determined, and if the condition is true, the loop body continues to execute,
If the condition is false, the loop ends.
* C: Features
* Unconditional First execution
* B: Case code
public class dowhiledemo{
public static void Main (string[] args) {
int i = 0;
do{
System.out.println (i);
i++;
}while (I < 5);
}
}


# # #14死循环
* A: Dead Loop Overview
* The reason for the existence of infinite loops is not knowing how many cycles, but depending on certain conditions, to control the loop
* B: Dead loop format
* while (true) {}
* for (;;) {}


# # #15嵌套for循环_1
* A: Overview of Nested Loops
* Nested loops refer to the syntax structure of a looping statement that is defined in the loop body of a loop statement. While, Do...while, and for loop statements can be nested, and they can also be nested among themselves, such as the most common nested for loops in a for loop.
* B: Format of Nested loops
For (initialize an expression; loop condition; action expression) {
.........
For (initialize an expression; loop condition; action expression) {
EXECUTE statement
.........
}
.........
}
* C: Explanation of each module
* Total cycle times = number of cycles in the outer loop
* Inner circulation, is the circulation body of outer circulation

* Outer loop, control is the number of rows
* Inner loop, which controls the number of rows per line

# # #16嵌套for循环_2
* A: Case
* A: Print positive triangles
* B: Case code
public class forfordemo{
public static void Main (string[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; J < i+1; J + +) {
System.out.print ("*");
}
System.out.println ();
}
}
}


# # #17break语句
* A:break Statement
* A: function
* Jump out of your loop body
* B: Writing position
* must appear within the loop or selection structure
* C: Example
for (int i=0; i<10; i++) {
if (i>5) {
Break
}
System.out.println ("I love Java" +i);
}
Will output 6 times from 0-5 "I love Java"
* B:break Detailed explanation
* A: function
* In the Loop/switch selection or loop process, we always satisfy the Boolean expression condition in order to execute the corresponding code, but in these logical processes,
You can use some keywords to jump directly out of the code you are executing, to execute the code behind or at the specified location,
Once these keywords appear, you can jump to the order in which the statements are executed.
* B: How to use
* Cannot be used alone, you must place the break keyword in a switch or loop statement
* C: Operating rules
* Do not need to judge any conditions, as long as you encounter a break change directly out of the execution of subsequent code. Will completely jump out of the selection or loop structure
* Can only jump out of the nearest block of code, cannot span multiple blocks of code

* C: Cyclic marking
* A: Why use cyclic marking
* When there is a switch SELECT statement in a double loop or loop, we find that the object using break or continue is an inner statement and cannot jump out of the outer loop directly, then we need to use the label statement to jump.
* B: How to use
* In front of a line outside the outer loop, use the identifier followed by the colon ":", which is defined.
When used, when the inner loop uses break or continue, the label immediately following the previous definition
* C: Operating rules
* Labels are defined outside the outer loop
* The inner layer uses break to terminate internal and external double loops.
* The inner layer uses continue to terminate the inner loop and continue the outer loop.
# # #18continue语句
* A:continue Statement
* A: function
* End this cycle ahead of time and proceed to the next cycle
* B: How to use
* Cannot be used alone, the Continue keyword must be placed in a looping statement
* C: Operating rules
* Do not need to judge any conditions, as long as the encounter continue change directly out of this round cycle for the next cycle
* D: Case code
public class continuedemo{
public static void Main (string[] args) {
for (int i = 0; i < i++) {
if (i%2==0) {
Continue
}
System.out.println (i);
}
}
}
Will print all the odd numbers between 0-9 to the console.


# # # #19猜数字小游戏
* A: Guess number games
* A: Analysis
* User-given number may be greater than, less than, or equal to the number of guesses, so there will be three cases, with the preceding ternary operator can be implemented,
but with the ternary operator nesting, More Trouble! Can be judged in a simpler way if condition, can have more than three conditions
* B: Requirement Analysis
* Background pre-generated a random number 1-100, user keyboard input guessing number
* If you guessed right, print "Congratulations, correct it"
* if you guessed wrong
* Guess big: print "Sorry, you guessed big!"
* Guess small: print "Sorry, you guessed small!"
until the number is guessed
can only guess up to 5 times, otherwise prompt "Sorry, you have no chance!"
* B: Case code
/*
Guess number Games

Complete guess number games:
1, generate random number
Background pre-generate a random number 1-100, user keyboard input guess number
2, Compare the number of guesses to the random number by the IF statement
if you guessed it, print "Congratulations, correct it"
if you're wrong,
guess big: Print, sorry, you guessed it!
Guess small: print "Sorry, you guessed small!"
3, the loop of the user guessing through the for loop
until the number is guessed
can only guess up to 5 times, otherwise prompt "Sorry, you have no chance!"

*/
Import java.util.Random;
Import Java.util.Scanner;
//You can import all the classes under the package one at a time, but it is not recommended. Which one to use is recommended.
//import java.util.*;
public class guessnumber{
public static void Main (string[] args) {
//1, Generate random number
//Background pre-generate a random number 1-100, User keyboard input Guess number
//create random Number object
Random random = new random ();
Generates a 1-100 random number
int randomnumber = Random.nextint (+1);
System.out.println ("I'm generating random numbers:" +randomnumber+ "What do you guess? "); Cheat-only

Scanner object that produces console input
Scanner sc = new Scanner (system.in);
3, through the for loop to complete the user guessing the number of loops
Using the For loop to finish guessing the digital logic
for (int i=1; i<=5; i++) {
Prompts the user to enter the number to be guessed, receives with the variable
System.out.println ();
System.out.println ("Please enter a number of 1-100:");
int guessnumber = Sc.nextint ();

2. Compare the number of guesses with the random number by the IF statement
If that's the right guess.
if (Guessnumber==randomnumber) {
Print the tips after guessing the right
System.out.println ("Congratulations, you guessed it!") ");
Jump out of the loop, no more guessing.
Break
}else {//if the wrong guess
If it's a big guess,
if (Guessnumber>randomnumber) {
System.out.println ("Sorry, you guessed big!");
}else {//If the Guess is small
System.out.println ("Sorry, you guessed small!");
}
}
If I guessed the last 5th time, and still don't guess right, jump out of the loop.
if (i==5) {
System.out.println ("Sorry, too back, come again next time!") ");
Break
}
Every time I guess wrong, I'll be sure how many chances.
SYSTEM.OUT.PRINTLN ("Please note that you also have a" + (5-i) + "chance, please answer carefully!) ");
}
}
}

Third day of the Java Foundation

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.