Day04-java-loop structure (while, do-while, For)

Source: Internet
Author: User

loop structure (while, do-while, For)

Any complex program logic can be implemented by three Structures:
1) sequential Structure: execute from top to bottom, every sentence must go
2) branching structure: conditional execution of a statement once, not every sentence must go
3) Loop structure: Conditional execution of a statement multiple times, not every sentence must go


1. looping: repeatedly executing a code of the same or similar


2. Cyclic three elements:
1) initialization of cyclic variables
2) conditions of the loop (based on the cyclic variable)
3) changes in cyclic variables (toward the end of the Cycle)
Loop variable: The amount of change in the loop


3. Loop structure:
1) while: first judgment after execution, it is possible not to execute at once
2) do...while: first execution after judgment, at least once
The 1th element is preferred with the 3rd element
3) for: Highest application rate, fixed number of cycles


4.break: jump out of the loop
Continue: skips the remaining statements in the loop body and goes to the next loop

Math.random ():
Math.random ()-------------0.0 to 0.9999999999 ...
*1000---------------------0.0 to 999.99999999 ...
+1------------------------1.0 to 1000.9999999 ...
(int)---------------------0 to 999
+1------------------------1 to 1000

for-random addition Arithmetic
 packageday04;Importjava.util.Scanner;//random addition arithmetic public classaddition { public Static voidmain (string[] Args) {Scanner scan=NewScanner (system.in); intScore = 0;//Total         for(intI=1;i<=10;i++) {//10 plays            intA = (int) (math.random () *100);//Addend 1            intB = (int) (math.random () *100);//Addend 2            intresult = a+b;//and theSystem.out.println ("+i+") "+a+" + "+b+" =? ");//1. QuizSystem.out.println ("count It!")); intAnswer = Scan.nextint ();//2. Answer            if(answer==-1) {//3. The award questionSystem.out.println ("the Next time you come Again!"));  break; }            if(answer==Result) {score+ = 10;//10 points to answer the question .System.out.println ("correct it"); }Else{System.out.println ("wrong answer."); }} System.out.println ("score=" +score); }}
while Loop demo:
 package day04; // demo of While Loop  public class addition {    publicstaticvoid  main (string[] Args) {          int num = 1;          while (num<10) {            System.out.println (num+ "*9=" +num*9);            Num+ +;        }        System.out.println ("over");}    }
Do and loop demo:
 packageday04;Importjava.util.Scanner;//Guess number Games public classaddition { public Static voidmain (string[] Args) {Scanner scan=NewScanner (system.in); intnum = (int) (math.random () *1000+1);//random number within 1 to 1000System.out.println (num);//Cheating        intguess;  do{System.out.println ("guess it!"); Guess= Scan.nextint ();//1,3            if(guess==0){                 break; }            if(guess>Num) {System.out.println ("too big."); }Else if(guess<Num) {System.out.println ("very small."); }        } while(guess!=num);//2        if(guess==Num) {System.out.println ("congratulations, you guessed it!"); }Else{System.out.println ("nice Place to stay"); }    }}        
Sum of series
Calculation of the 1+2+3+4+5...+100;
 package   day04;  //  calculates 1+2+3+4+5...+100 's and;  import   java.util.Scanner;  public  class   addition { public  static  void   main (string[] Args) {        /span>int  num = 0;  for  (int  I=1;i<=100;i++ = num + i;    } System.out.println (num); }} 
result: 5050

There are several columns: 9,99,999,...,9999999999. Requires a program to calculate the and output of this sequence, and outputs the results in the Console:

     public classSumofseq { public Static voidmain (string[] Args) {//Sum of series            LongNine = 9; Longresult =nine;  for(inti = 2; I <= 10; i++) {nine= Nine * 10 + 9; Result+=nine; } System.out.println ("9+99+999+...+9999999999=" +result); }    }

Another series: 1+1/2+1/3...+1/n (n>=2). It is required to use interactive methods to calculate the number of this series and: the user in the console input needs to calculate the value of the integer n, The program calculates the summation of this sequence, and the output results in the Console.

    Importjava.util.Scanner;  public classSUMOFSEQ2 { public Static voidmain (string[] Args) {Scanner Scanner=NewScanner (system.in); System.out.println ("please Enter an integer (for example 10):"); intn =Scanner.nextint (); Doubleresult = 0;  for(inti = 1; I < n; i++) {result+ = 1.0/i; if(i==1) {System.out.print ("1+"); }Else{System.out.print ("1/" + i + "+"); }} result+ = 1.0/n; System.out.print ("1/" + n + "=" +result); }    }

Day04-java-loop structure (while, do-while, For)

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.