Java Second JoJo Learning summary

Source: Internet
Author: User

Java Operators and Loops

Java operators

A. For statement

The basic structure of the For statement is as follows:
For (initialize an expression; Judge an expression; increment (decrement) an expression) {
Execute the statement; A section of code
}

    • Initialization expression: The meaning of the initialization expression is to define the value of the variable before the loop is defined, and if it does not, it does not know which value to start the loop from.
    • Judgment expression: The function of judging an expression is to specify the end point of the loop. If the expression is not judged, then the loop becomes a dead loop.
    • Increment (decrement) expression: This specifies how much increment or decrement the variable will be in each execution of the program.


Example: Calculate the result of a 1+2+3+4......+100.
public class control5{
public static void Main (string[] args) {
int result=0;
for (int i=1;i<=100;i++) {
Result+=i;
}
SYSTEM.OUT.PRINTLN (result);
}
}
Output Result:
5050

Two. While loop

In English, the word "while" means "when", while in Java programming, it can also be understood as "when" and its grammatical structure is:
while (condition) {
Purpose A section of code
}
When the condition is true, enter the loop.

Example: Calculate the result of a 1+2+3+4......+100.
public class control5{
public static void Main (string[] args) {
int a=1,result=0;
while (a<=100) {
result+=a++;
}
SYSTEM.OUT.PRINTLN (result);
}
}
Output Result:
5050

Three. Do/while statements

Before you learn the Do/while statement, you should know how the while statement works. The while statement performs a conditional judgment before executing the loop body inside the curly braces.

The Do/while statement differs from the while statement by executing the loop body inside the curly braces and then judging the condition if the condition is not satisfied and the loop body is not executed the next time. In other words, the loop body within the curly braces is executed before the condition is judged.
Example: Calculate the result of a 1+2+3+4......+100.
public class control5{
public static void Main (string[] args) {
int a=1,result=0;
bo=
result+=a++;
}while (a<=100);
SYSTEM.OUT.PRINTLN (result);
}
}
Output Result:
5050

Use of Java operators and loops

Example 1

class lai1{    publicstaticvoid  main (string[] args)     {            int a =1;          while (a<=100)        {                if (a==10)            {                break;            }            System.out.println (a);                        A+ +;                     }}}

Write a while loop, and this loop stops at the nineth time

import java.util.*; class lai2{    publicstaticvoid  main (string[] args)     {        new  Random ();         int a = Random.nextint (a);        System.out.println (a);    }}

2. Generate a random number within [0-20], including 20

ImportJava.util.*;classLai8 { Public Static voidMain (string[] args) { for(intC=0;; ) {Random random=NewRandom (); intA = Random.nextint (11) +10; intb = Random.nextint (11) +10; System.out.println (A+ "+" +b+ "=" + "?"); System.out.println ("Please enter the answer:"); Scanner input=NewScanner (system.in); intD =Input.nextint (); if(d== (A +b)) {C++; System.out.println ("You've answered the +c+ question."); }Else{System.out.println ("Failure is the mother of success."); }            if(c==10)            {                                     Break; }                    }                    }}


3. Randomly generate a number of two 10-20 to do operations (+-*/) until the end of the 10-question.

There are a lot of ways to use it, not one by one demonstrations.

This week is the most important cycle of learning, feel the cycle is difficult to learn, always can not find ideas to write, after a lot of practice!

Java Second JoJo Learning summary

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.