One, two common tools
1. Randomly produce a number between [0,1]
math.random ()
Example:
Double box =math.random ();
Generate random decimals [0,1]
System.out.println ("box=" +box);
[Min,max+1]
int min = 5;
int max = 9;
Box =math.random () * (max-min+1) +min;
SYSTEM.OUT.PRINTLN (box);
2, to interact with customers, to obtain the input value of the customer's computer
Java.util.Scanner
Specific steps:
1. Positioning Package: Import Java.util.Scanner
2, Preparation: Scanner tool =new Scanner (system.in)
3, use: Tool.nextint (), tool.nexdouble
Example:
Import Java.util.Scanner;
public class tooldemo02{
public static void Main (string[] args) {
2. Preparation Tools
Scanner tool =new Scanner (system.in);
3. Use
1), get integer
System.out.println ("Please enter Age:");
int age = Tool.nextint ();
System.out.println ("The Age you entered is:" +age);
2), Get decimal
System.out.println ("Please enter elevation:");
Double height =tool.nextdouble ();
System.out.println ("The elevation you entered is:" +height);
Second, type conversion
Cause, the data type does not match, in order to match the problem, the type needs to be processed, classified as
Automatic type conversion
Large Type box = small type of data
1, if there is a large number of participation in the operation, the result is a large number of whichever, as far as possible to advance the large number.
2. Within the range of tables: byte short char = literal | constant value
3. When arithmetic:
1), literal | constant value
byte| Short| Char|int =byte Short Char int +byte short char int
2), once a variable is involved in the operation
int =byte short char int +byte short char int
Forcing type conversions
Small Type box = (small type) large type of data;
Note () Scope of action
Example:
/**
Coercion Type conversions: small type variable = (small type) large type of value
Attention point: () Scope of action
*/
public class parsedemo02{
public static void Main (string[] args) {
char ch = ' A ';
int box = CH; Automatic
ch = (char) box; Forced
Double num = 3.5;
box = (int) num; Discard decimal Points
SYSTEM.OUT.PRINTLN (box);
() Scope of action
box = (int) (NUM*2); (int) num*2
SYSTEM.OUT.PRINTLN (box);
generating [0,8] random integers
int max = 8;
int min = 0;
int letter = (int) math.random () * (max-min) +min;
SYSTEM.OUT.PRINTLN (letter);
Correct wording
letter = (int) (Math.random () * (max-min) +min);
SYSTEM.OUT.PRINTLN (letter);
Generate a random lowercase letter capital Letter
97+0--> ' a '
97+1--> ' B '
97+25--> ' Z '
max = ' Z ' +1;
min = 97;
letter = (int) (Math.random () * (max-min) +min);
System.out.println ((char) letter);
' A '
Generate lowercase letters (solve unknown problems based on existing tools)
Char lowerletter = (char) (Math.random () * (' Z '-' a ' + 1) + ' a ');
System.out.println (Lowerletter);
Think about how to generate capital letters
}
}
III. Choice of implementation
Execute at most once
1, if: Conditional boolean value, General interval
"If so," the condition is set up and implemented, divided into three categories
1), Single choice
If (condition) { Execute code block } |
Example:
/**
Single choice:
if () {
}
Generates an integer [0,100] that determines whether an even number is the output even
*/
public class ifdemo01{
public static void Main (string[] args) {
1. generating [0,100] integers
int rnd = (int) (Math.random () *101);
System.out.print ("The number produced is:" +rnd);
2. Whether the number is even, if it is even, the output is "even"
Analysis: Even rnd%2==0
/*
Boolean flag = (rnd%2==0);
Apply Selection
if (flag) {
SYSTEM.OUT.PRINTLN ("This number is even");
}
*/
At development time, if the variable is used only once, you can anonymously
if (rnd%2==0) {
SYSTEM.OUT.PRINTLN ("This number is even");
}
}
}
2), Double choice
If (condition) {
Executing code block 1
}else{
Executing code block 2
}
Example:
/**
Dual choice:
if () {
}else{
}
Generates [0,100] integer, determines whether an even number, if the output is an even number, otherwise the output is odd
*/
public class ifdemo02{
public static void Main (string[] args) {
1. generating [0,100] integers
int rnd = (int) (Math.random () *101);
System.out.print ("The number produced is:" +rnd);
2. Whether the number is even, if it is even, the output is "even"
At development time, if the variable is used only once, you can anonymously
if (rnd%2==0) {
SYSTEM.OUT.PRINTLN ("This number is even");
}else{
SYSTEM.OUT.PRINTLN ("This number is odd");
3), Multi-Select
If ( condition 1) {
// executing code block 1
}else if (condition 2) {
Executing code block 2
}else if (condition 3) {
Executing code block 3
}else{
}
Example
/**
Multiple selection: Enter an integer [1,7] from the keyboard
1. Decide whether it is a weekend or a weekday
*/
Import Java.util.Scanner;
public class ifdemo03{
public static void Main (string[] args) {
Scanner Tools =new Scanner (system.in);
Enter 1-7 integers from the keyboard
int day =tools.nextint ();
if (day==1) {
System.out.println ("Sad reminder weekday");
}else if (day==2) {
System.out.println ("Sad reminder weekday");
}else if (day==3) {
System.out.println ("Sad reminder weekday");
}else if (day==4) {
System.out.println ("Sad reminder weekday");
}else if (day==5) {
System.out.println ("Sad reminder weekday");
}else if (day==6) {
System.out.println ("boring Weekend");
}else if (day==7) {
System.out.println ("boring Weekend");
}
System.out.println ("A Good Life");
if (Day>=1 &&day<=5) {
System.out.println ("Sad reminder weekday");
}else if (day>=6 && day<=7) {
System.out.println ("boring Weekend");
}else{
SYSTEM.OUT.PRINTLN ("wrong input ... ");
}
}
}
2. Switch: Fixed value judgment
Structure
switch (int integer |1.7 string | enum) { Case fixed value 1 (literal | constant): Code 1 Break Case Fixed value 2 (literal | constant): Code 2 Break ...... Default: Other conditions } |
Be aware that:
1. Switch fixed value expression
2. Case fixed Value: constant Literal enumeration value
3, default: Generally put in the last, the position is not fixed
4, Break: End switch to prevent the next wear
End: Normal to} encountered break
Example
Import Java.util.Scanner;
public class switchdemo03{
public static void Main (string[] args) {
Scanner tool =new Scanner (system.in);
Receiving month [1,12]
System.out.println ("Please enter the month:");
Int month =tool.nextint ();
Switch (MONTH/3) {
Case 1:
System.out.println ("Chunkun");
Break
Case 2:
System.out.println ("Summer nap");
Break
Case 3:
System.out.println ("Autumn is spent");
Break
Default
System.out.println ("hibernation");
}
The above is a simple form, the following is a complex form.
/*
Switch (month) {
Case 3:
Case 4:
Case 5:
System.out.println ("Chunkun");
Break
Case 6:
Case 7:
Case 8:
System.out.println ("Summer nap");
Break
Case 9:
Case 10:
Case 11:
System.out.println ("Autumn is spent");
Break
Default
System.out.println ("hibernation");
}
*/
}
}
Java Learning next day