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
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 + "");
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
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
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); }
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
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 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
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
: 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
$ = 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
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.