java while loop example

Discover java while loop example, include the articles, news, trends, analysis and practical advice about java while loop example on alibabacloud.com

Java,for loop nesting, print diamond

Public classT8 { Public Static voidMain (string[] args) {//Upper Part for(intm = 1; M ) { //Output Spaces for(intn = 9; n >= m; n--) {System.out.print (" "); } //Output * Upper left for(intn = 1; n ) {System.out.print ("*"); } //Top Right for(intn = 1; n ) {System.out.print ("*"); } System.out.println (); } //Lower Half for(intm = 1; M ) { //Output S

Java loop output a diamond and factorial countdown

Package Javafirst;public class Homework {public static void main (string[] args) {System.out.println ("Output a diamond! "); for (int i = 0; I   Java loop output a diamond and factorial countdown

Java for each loop array object traversal

Java for Each loop array object traversal Grammar for (type Itr-var:iterableobj) statement-block Take a look at the enumeration group instances public class MainClass {public static void Main (string args[]) {int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};for (int x:nums) {System.out.print (x + "");x = x * 10; No effect on Nums}System.out.println (); for (int x:nums)System.out.print (x + "");

Java: Two Methods of set loop Traversal

Package collection; Import java. util .*; Public class newset {Public static void main (string [] ARGs ){Set For (INT I = 0; I Students. Add (new student ("happy" + I, "male" + I, 20 + I ));}Students. Add (new student ("happy0", "male0", 20 ));Print (students );Print2 (students );}// Loop traversal ①Public static void print (set System. Out. println ("total data:" + newlist. Size ());/* Student;For (INT I =

Implementation of the Java loop queue

; } Else { returnfalse; } }Take the team head element  Public void queuefront (int getfront) { if(isEmpty () = =false) { Getfront=queuelist[(front+1)%maxlen]; } Else { System.out.println ("Error:queue is Empty"); return ; } }Team Public void enQueue (int endata) { if(isfull ()

Java loop intercepts string, stored in list

String interception to the list using the base transform file store may use Public voidDemo () {String str= "abcdefgfdsdgdgdgdh1f"; ListNewArraylist(); String streee= ""; intLen =str.length (); intSub = 3; if(len% Sub! = 0) { for(inti = 0; I ) {streee= Str.substring (i, i + 3); List.add (STREEE); } String TPM= Str.substring (Str.length ()-(len%sub), Str.length ()); List.add (TPM); } Else { for(inti = 0; I ) {streee= Str.substring (i, i + 3);

Java small case--using a dual for loop to implement the output of the Yang Hui Triangle

each row has been assigned.//The current value--the number of the column on the previous row + the first number on the left of the column on the previous line (see image)Arr[i][j]=arr[i-1][j]+arr[i-1][j-1]; } } //Print Output for(inti = 0; i ) { for(intj = 0; J ) {System.out.print (Arr[i][j]+ "\ T");  No Line break} System.out.println (); Line Change}}}Operation Result:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 5 1

Java base _ Enhanced for loop: foreach

In general, access to data in List,array,set is accessed in the following wayint [] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten }; for (int i = 0; i ) { System.out.println ("i =" + i);}Since Java1.5, there has been a more convenient way to access: foreachint [] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten }; for (int x:nums) { System.out.println ("Value is:" + x); + = x;} Grammar: For (type variable name: Collection variable name) {statements;} Precautions

The 4th chapter writes the Java program, applies the For loop to print the diamond

Package four ; Public classFouroneone { Public Static voidMain (String args[]) { for(intI=1; i7; i+=2){ for(intkong=7; kong>i-1; kong--) {System. out. Print (" "); } for(intxing=1; xing) {System. out. Print ("* "); } System. out. println (); } for(intj=1; j5; j+=2){ for(intkong1=1; kong13; kong1++) {System. out. Print (" "); } for(intxing1=5; xing1>=j;xing1--) {System. out. Print ("* "); } System. out. println (); } }}The 4th cha

The 4th chapter writes the Java program, uses the while loop statement to calculate the sum of 1+1/2!+1/3!+...+1/20!

Package four;public class Fouronetwo {public static void Main (String args[]) {Double sum = 0,a = 1;int i = 1;while (I {sum = sum+a;i = i+1;A = A * (1.0/i);}SYSTEM.OUT.PRINTLN (sum);}}Explanation: When I=1, Sum=1, i=2, a=1* (1/2);When i=2, SUM=1+1*1/2, I=3, a=1*1/2* (1/3);When I=3, SUM=1+1*1/2+1*1/2*1/3, i=4, a=1*1/2*1/3* (1/4) ...When I=20, SUM=1+1*1/2+1*1/2*1/3+...+1*1/2*...*1/20, i=21, a=1*1/2*...* (1/21)When I=21 is not establishedThe 4th chapter writes the

java--jdk1.5 New Enhanced for loop

array: The subscript value cannot be easily accessed. Collection: It is not easy to delete things in the collection compared to using Interator. inside is also called InteratoIn addition to simply traversing and reading out the contents, it is not recommended to use the enhanced for. "Program Analysis"int[] arr = {1,2,3,4,5};for (int i:arr) {System.out.println (i); }Collection C = new ArrayList ();C.add (New String ("AA"));C.add (New String ("BB"));For (Object o:c) {System.out.println (o); }

Java Loop nesting Exercises

Yi Shi Heavy(1) The output results are as follows:11 21 2 31 2 3 41 2 3) 4 51 2 3 4 5 61 2 3 4 5 6 71 2 3 4 5 6 7 81 2 3 4 5 6 7 8 9 Public class Test { publicstaticvoid main (string[] args) { int i,j,; for (i = 1;i ) { for (j = 1;j i;j++) { System.out.print (j); } System.out.println (); }}}(2) The output results are as follows:9 8 7 6 5 4 3 2 18 7 6 5 4 3 2 17 6 5 4 3 2 16 5 4 3 2 15 4 3) 2 14 3 2 13 2 12 11(3)1 2 3 4 5 61

The Java Foundation does while loop

Tag: the base port exti new Int () defines the ring stringhttp://www.verejava.com/?id=16992623980921/* do{ }while(判断表达式) : 直到 while 判断表达式为 false 退出循环*/import java.util.Scanner;public class Test1 { /** * @param args the command line arguments */ public static void main(String[] args) { // 模拟切水果游戏, 键盘输入 1:西瓜,2:香蕉,3:水蜜桃,-1:雷 //输入数字打印对应的水果, 如果输入-1 终止游戏输入 Scanner in=new Scanner(System.in); int num=0; //定义了一个变量 do { num=in.n

How to delete elements in a collection in a Java loop

public void Remove () {listHow to delete elements in a collection in a Java loop

Java DBCP Connection pool, large data processing loop multiple table operation insert case

= new file (FilePath);BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (new FileInputStream (file), "Utf-8"));String line = null;while (line = Bufferedreader.readline ()) = null) {line = Line.trim ();Buffer.append (line);}Bufferedreader.close (); Return Removenotes (Buffer.tostring ());} /*** Remove Comments** @date 3:38:10* @author Duchaowei* @descripte* @param str* @return*/private string Removenotes (String str) {int start = Str.indexof ("//#");int end = Str.indexof

Branching statements in Java--classification of program run flow (sequential structure, branch structure, loop structure)

There are three main types of program running in Java:1, sequential structure: Sequential structure is the execution of each line of code sequentially2, branching structure: branch structure is branching according to different conditions3, loop structure: A piece of code is executed according to the conditions of the loop.Of these, there are two main types of branching structures:If...else ... Structure and

Java design Polling thread performance issues with while loop

Java Design Polling thread performance issues with while loopThe polling thread is widely used in the development process, where I simulate a scenario that has a queue and polling thread, the main thread queues messages into the queue, and the polling thread loops through the queue to read the message and print the message content. A bit like handler in Android sends messages. First define a message class.public class Message { private String conte

Java Loop structure (i) some small examples of the use of a while for Do...whiile statement

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;i5.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;i6.public class Text6 {public static void mai

java-Conditional statement, loop statement practice

: Console input age, according to age output different tipsScanner sc=new Scanner (system.in);System.out.println ("Please enter an Age:");int Age=sc.nextint ();if (age>60){System.out.println ("You are old");}else if (age>30){System.out.println ("You are middle-aged");}else if (age>18){System.out.println ("You for Youth");}else if (age>13){System.out.println ("You are a teenager");}Else{System.out.println ("You Are Children");} Topic Seven: Enter the radius of the circle, calculate and outp

Enhanced for loop foreach in Java

$ = array$.length, i$ = 0; i${int i = array$[i$];{System.out.println(i);}}List list = new ArrayList();list.add(1);list.add(2);list.add(3);for(java.util.Iterator i$ = list.iterator(); i$.hasNext();){String s = (String) i$.next();{System.out.println(s);}} It is clear that:1. For arrays, the Foreach loop actually uses the normal for loop2. For collections, the Foreach loop is actually an iterative iter

Total Pages: 15 1 .... 11 12 13 14 15 Go to: Go

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.