Note the point:
The while loop is first judged and then executed.
Do.....while first to perform a second judgment.
The break statement ends the loop directly.
The continue statement can only be used inside the loop, and the function is to skip the loop and continue with the next loop.
Here are some examples of programs:
1, Enter an integer n, output 1+2+... +n the and.
Import Java.util.scanner;public class Text1 {public static void Main (string[] args) { Scanner sc=new Scanner ( system.in); int i,n,sum=0; N=sc.nextint (); for (i=1;i<=n;i++) sum+=i; SYSTEM.OUT.PRINTLN (sum);} }
2.input from keyboardTenintegers, calculating their and, product, and average values.
Import Java.util.scanner;public class Text2 {public static void Main (string[] args) { Scanner sc=new Scanner ( system.in); int i,j,sum=0,s=1,aver; for (j=1;j<=10;j++) { i=sc.nextint (); sum+=i; s*=i; } AVER=S/10; System.out.println ("sum =" +sum+ "Product =" +s+ "mean =" +aver);} }
3.Enter an integer to determine the number of digits.
Import Java.util.scanner;public class Text3 {public static void Main (string[] args) { Scanner sc=new Scanner ( system.in); int i,j=0; I=sc.nextint (); while (i!=0) { i=i/10; j + +; } System.out.println (j); }}
4.with forStatement Calculation1~100the sum of the odd numbers between.
Import Java.util.scanner;public class Text4 {public static void Main (string[] args) { Scanner sc=new Scanner ( system.in); int i,sum=0; for (i=0;i<=100;i++) { if (i%2==1) sum+=i; } SYSTEM.OUT.PRINTLN (sum);} }
5.Calculation2+22+222+2222+22222+.........+nthe sum.
Import Java.util.scanner;public class Text5 {public static void main (string[] args) { Scanner sc=new Scanner (System. in); int n,i,j=0,sum=0; N=sc.nextint (); for (i=1;i<=n;i++) { j=j*10+1; sum+=j; } sum=sum*2; SYSTEM.OUT.PRINTLN (sum);}}
6.
public class Text6 {public static void main (string[] args) { int i; for (i=1;i<=9;i++) if ((30+i) *8256== (i*10+3) *6528) System.out.println (i);}}
7.there is a fractional sequence: 2/1,3/2,5/3,8/5,13/8,21/13. Find the sum of the first 20 items of this series.
Import Java.util.scanner;public class Text7 {public static void main (string[] args) { Scanner sc=new Scanner (system.i n); Double a,i=1,j=0,sum=0,s; int n; N=sc.nextint (); for (a=1;a<=n;a++) { s= (i+a)/(j+a); sum+=s; } SYSTEM.OUT.PRINTLN (sum);}}
8.Write the program implementation, the input n is an even number, 1/2+1/4+...+1/n, when the input n is an odd number, seek 1/1+1/3+...+1/n.
Import Java.util.scanner;public class Text8 {public static void main (string[] args) { Scanner sc=new Scanner (System. in); Double i,sum=0; int n; N=sc.nextint (); if (n%2==0) {for (i=1;i<=n/2;i++) sum+=1/(2*i); } else {for (i=1;i<= (n+1)/2;i++) sum+=1/(2*i-1); } SYSTEM.OUT.PRINTLN (sum);}}
9.calculates the first n of the Fibonacci sequence.
<span style= "FONT-SIZE:18PX;" >import Java.util.scanner;public class Text9 {public static void main (string[] args) {Scanner sc=new Scanner (system.in ); int I=0,j=1,n,a,sum=0;n=sc.nextint (); if (n==0) System.out.println (i); else if (n==1) System.out.println (j); else{for (a=1;a<=n-2;a++) Sum+=i+j+i+j;} SYSTEM.OUT.PRINTLN (sum);}} </span>
Java Loop structure (i) some small examples of the use of a while for Do...whiile statement