Java "Looping structure" practice example

Source: Internet
Author: User
1, Output 1 to 100 of these 100 numbers (respectively, for, while and do-while implementation).
  	public class test{public static void Main (string[] args) {System.out.println ("For loop output:");
  	for (int i=1;i<=100;i++) {System.out.print (i+ "");  } System.out.println ("\ n");
  	Change two lines of System.out.println ("While loop output:");
  	int i=1;
  		while (i<=100) {System.out.print (i+ "");
  	i++;
  	
  	} System.out.println ("\ n");
  	System.out.println ("Do-while cycle output:");
  	int j=1;
  		do{System.out.print (j+ "");
  	j + +;
  }while (j<=100);
} 2, 1 to 100 and (for, while, and Do-while respectively).
  	public class test{public static void Main (string[] args) {System.out.println ("For loop output:");
  	int sum1=0;
  	for (int i=1;i<=100;i++) {sum1+=i;
  	
  	} System.out.println (SUM1);
  	System.out.println ("While loop output:");
  	int sum2=0;
  	int i=1;
  		while (i<=100) {sum2+=i;
  	i++;
  	
  	} System.out.println (SUM2);
  	System.out.println ("Do-while cycle output:");
  	int sum3=0;
  	int j=1;
  		do{sum3+=j;
  	j + +; }while (J&LT;=100);
  System.out.println (SUM3);
} 3, get the product of 1 to 10 (with either loop implementation).
  	public class test{public static void Main (string[] args) {int sum=1;
  	for (int i=1;i<=8;i++) {sum*=i;
  The product of System.out.println ("1 to 10 is:" +sum);
4, print out the following three types of 99 multiplication tables.
  	public class test{public static void Main (string[] args) {System.out.println ("First:");
  		  for (int i=1;i<=9;i++) {for (int j=1;j<=i;j++) {System.out.print (i+ "*" +j+ "=" +i*j+ ");	
  		if (i*j<10) System.out.print ("");
  	} System.out.println ();
  	} System.out.println ("Second:");
  		  for (int i=1;i<=9;i++) {for (int j=9;j>=i;j--) {System.out.print (i+ "*" +j+ "=" +i*j+ ");	
  		if (i*j<10) System.out.print ("");
  	} System.out.println ();
  	} System.out.println ("Third:");  for (int i=1;i<=9;i++) {for (int k=8;k>=i;k--) {//This loop is used to add a space System.out.print ("");
Add 7 spaces at a time for an expression of length} for (int j=1;j<=i;j++) {System.out.print (i+ "*" +j+ "=" +i*j+ ");  		  if (i*j<10) System.out.print ("");
  	} System.out.println ();
} 5, print the following format pyramid, the number of rows of the pyramid can be arbitrarily specified.
  	public class test{public static void Main (string[] args) {int level=10;
  		for (int i=1;i<level;i++) {for (int k=i;k<level-1;k++) {//This loop is used to add a space System.out.print ("");
  		for (int j=1;j<= (2*i-1); j + +) {System.out.print ("*");
  	} System.out.println ();
} 6, Output 1 to 1000 can be divisible by 7, each row shows 10, and the output number is separated by a space.
  	public class test{public static void Main (string[] args) {int j=1;
  		for (int i=7;i<=1000;i+=7) {System.out.print (i+ "");
  		if (j%10==0) {//is used to determine whether the output is 10 times, the word is changed line System.out.println ();
  	} j + +; }
  }
}

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.