Thousand Peaks Training Day03-java Basic study: For Loop, switch,if

Source: Internet
Author: User
Tags arithmetic operators bitwise operators logical operators

Course review:

Variable
Format: Data type variable name [= value];
A variable must be declared and then assigned before it can be used


Operator:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5, bitwise operators
6. String Join operator
7, trinocular operator


Today's content:
Structure of the program:
1, sequential structure: from top to bottom, progressive line
2. Branching structure: Selective execution code
If-else
Switch
3, loop structure: to meet a certain condition, has been executing code

To enter data from the keyboard:
1, Import java.util.scanner;//Guide package, at the top of the document
2. Inside the Main method
Scanner scn=new Scanner (system.in);//Create Object
int Score=scn.nextint ();//Gets the input number


Branching structure: Implement program flow control, choose to execute certain code according to certain conditions
1, If-else
Format one: if (boolean-type expression) {code block}, if ...
Executes the specified code block when the Boolean expression is True
Format two: if (boolean type expression) {code block}else{code block}, if ... Otherwise......
Executes the code of the IF statement if the condition satisfies, otherwise the code of the else is executed
Format three: if (Boolean expression) {code block}else if (Boolean expression) {code block}......else{code block}
Multiple conditions to meet one

Full format: if (Boolean expression) {code block}[else if (Boolean expression) {code block} ...] [else{code block}]
If can be used alone
else if and else are not allowed to be used alone and must be followed by the IF

Exercise: Enter age from the keyboard
If <18, output, underage
18-22: Youth
23-40: Middle Age
40-100: Old age
otherwise: unknown


Nested use of the IF statement:
Format:
If (Boolean-type expression)
{
......
If (Boolean-type expression) {...}
}


Exercise: int No, study number, if the study number is 8 digits, then the output congratulations you become a member of the thousand front, no output, unfortunately, the number is not correct
Please use 2 ways to complete the programming

Exercise: int age, char sex gender
If the male
Verify Age
If the age is between 18 and 28, output: Handsome boy
If a woman
Verification age, 18, output: female 18 change
Age for 20,30 output: glamour schoolgirl
Otherwise: Girls


2. Switch
Format:
Switch (exp)
{
Case value 1: code block;
Case Value 2: code block;
......
Case Value N: code block;
[Default: code block; break;]
}
Support for string after Exp:byte, short, char, int 1.6 Support after enumeration (enum) 1.7
Value: Constant value
Default: Optional, indicates that all case values do not match, and the default is executed

Simple Calculator
int num1=10,num2=6;
Char fh;//symbol
Entering symbols from the keyboard
Use switch to output results

Next (). charAt (0);

Leap year formula: Four years a run, a century not run, 400 years to run, February run 29 days, common year 28 days
Run year conditions:
1, four years a run, a century does not run
2, 400 years of re-run
/*
Variable, sex,age
Using If-else to verify gender
if: female
Use switch to verify the age
Age is divided by 30:
0 (Youth), 1 (Mature), 2 (Twilight), 3 (longevity)
if: Male
Age is divided by 50
0 (adult), 1 (old age), 2 (longevity)
*/

Loop structure:
If the condition is true, repeat the execution of the code.
1. For statement
Format: for (expression 1; expression 2; expression 3)
{
Circulation body;
}
Expression 1: Loop variable initialization
Expression 2: Cyclic condition validation (Boolean expression)
Expression 3: Cyclic variable change
Loop Body: A code block that executes repeatedly

2. While statement
3. Do/while statement


An even number within 10 of output

Prime number (prime number)

Tomorrow content
While
Do/while
Loop nesting
Special for statement
Special Cyclic statement control


1.
Input results from the keyboard, now the results are percentile
Output grade according to Grade
A:>=90
B:>=80
C:>=70
D:>=60
E:>=0

Import Java.util.Scanner;
Class Zuoye01
{
public static void Main (string[] args)
{
Declaring a guide-package variable
Scanner scan = new Scanner (system.in);
int score = Scan.nextint ();
If (Score < 60)
{
System.out.println ("Your grade is: E");
}
else if (Score < 70)
{
System.out.println ("Your grade is: D");
}
else if (Score < 80)
{
System.out.println ("Your grade is: C");
}
else if (score <90)
{
System.out.println ("Your grade is: B");
}
else if (score <100)
{
System.out.println ("Your grade is: A");
}
Else
{
SYSTEM.OUT.PRINTLN ("The input score has a problem, please re-enter");
}


}
}


2, please complete the grading of wages
Salary Range:
Wages
>20000: Wow, that's a high salary.
>10000: Yes, good.
>5000: Oh, I can survive.
>2000: Hey, you want some noodles?
Other: Only water.
1), nesting using the three-mesh operator
2), please use If-else
3), please use switch
Import Java.util.Scanner;
Class Zuoye02
{
public static void Main (string[] args)
{
Declaring a guide-package variable
Scanner scan = new Scanner (system.in);
int GJ = Scan.nextint ();//Receive keyboard input data

/** if (GJ > 20000)
{
System.out.println ("Wow, high salary yo");
}
else if (GJ > 10000)
{
System.out.println ("Yes, good Yo");
}
else if (GJ > 5000)
{
System.out.println ("Oh, can Survive");

}
else if (GJ > 2000)
{
System.out.println ("Hey, want to eat noodles Yo");
}
Else
{
System.out.println ("Only drink boiled water");
}
*/
/**
String A;
if (GJ < 5000)
{
A = GJ > 2000? "Hey, want to eat noodles yo": "Only drink water";
System.out.println (a);
}
else if (GJ < 20000)
{
A = GJ > 10000? "Yes, good yo": "Oh, can Survive";
System.out.println (a);
}
Else
{
System.out.println ("Wow, high salary yo");
}
*/

if (GJ >10000)
{
int a = gj/10000;
Switch (a)
{
Case 1:system.out.println ("Yes, good Yo");
Case 2:
Case 3:
Case 4:
Case 5:
Case 6:
Case 7:
Case 8:
Case 9:
Default:System.out.println ("Wow, high salary yo");
}
}
else if (GJ > 2000)
{
int b = gj/1000;
Switch (b)
{
Case 2:
Case 3:
Case 4:system.out.println ("Hi, eat noodles");
Case 5:
Case 6:
Case 7:
Case 8:
Case 9:system.out.println ("Oh, can Survive");

}
}
Else
{
System.out.println ("Water only");
}
}
}


3. Enter the year and month you want to query from the keyboard
Outputs the number of days in the corresponding month, and calculates the total number of days from the current January to the month


4, please output the odd number within 20
for (int i =1; i <=; i++)
{
if (i%2!=0)
{
System.out.println ("The odd number within 20 is:" +i);
}
}

5, please output 100 to 888 can be divisible by 13,
But numbers that can't be divisible by 3
if (100<m<888)
{}
else{input data error, please re-enter}

Requirements: Qualifying numbers, 4 per line
Note: System.out.println (): Output will wrap
System.out.print (): Output does not wrap
\ t: tab, which results in multiple spaces
Class Zuoye04
{
public static void Main (string[] args)
{
int b=0;
for (int i=100; i<=888; i++)
{
if (i%13==0)
{
if (i%3!=0)
{
System.out.print (i+ "\ t");
B+=1;
if (b%4==0)
{
System.out.println ("");
}
}
}

}
}
}


Additional Questions * * *:
Printing multiplication Formulas
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
......

Class Zuoye06
{
public static void Main (string[] args)
{
int num = 0;
for (int i=1; i<=9; i++)
{
for (int j=1; j<=i; j + +)
{
num = i*j;
System.out.print (i+ "*" +j+ "=" +num+ "\ t");

}
System.out.println ();
}
}
}

Thousand Peaks Training Day03-java Basic study: For Loop, switch,if

Related Article

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.