{"id":706,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java for\u5faa\u73af","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/706.html","title":{"rendered":"Java for\u5faa\u73af"},"content":{"rendered":"
\n
for\u5faa\u73af<\/span> for<\/span> \u521d\u59cb\u5316<\/strong>: \u8fd9\u662f\u5faa\u73af\u5f00\u59cb\u65f6\u6267\u884c\u4e00\u6b21\u7684\u521d\u59cb\u6761\u4ef6\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u53ef\u4ee5\u521d\u59cb\u5316\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u53d8\u91cf\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u9009\u6761\u4ef6\u3002<\/span> <\/body>
\n while\u5faa\u73af <\/span>
\n do-while\u5faa\u73af <\/span>
\n <\/p>\n
Java For\u5faa\u73afvs While\u5faa\u73afvs Do While\u5faa\u73af<\/h2>\n
\n\n
\n for<\/td>\n while<\/td>\n do while<\/td>\n<\/tr>\n \n Java for\u5faa\u73af\u662f\u4e00\u4e2a\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u5b83\u591a\u6b21\u8fed\u4ee3\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u3002 <\/td>\n Java while\u5faa\u73af\u662f\u4e00\u4e2a\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u5b83\u6839\u636e\u7ed9\u5b9a\u7684\u5e03\u5c14\u6761\u4ef6\u91cd\u590d\u6267\u884c\u90e8\u5206\u7a0b\u5e8f\u3002<\/td>\n Java do while\u5faa\u73af\u662f\u4e00\u6761\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u8be5\u8bed\u53e5\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u4e14\u8fdb\u4e00\u6b65\u6267\u884c\u53d6\u51b3\u4e8e\u7ed9\u5b9a\u7684\u5e03\u5c14\u6761\u4ef6\u3002<\/td>\n<\/tr>\n \n \u5982\u679c\u8fed\u4ee3\u6b21\u6570\u56fa\u5b9a\uff0c\u5efa\u8bae\u4f7f\u7528for\u5faa\u73af\u3002<\/td>\n \u5982\u679c\u8fed\u4ee3\u6b21\u6570\u4e0d\u56fa\u5b9a\uff0c\u5efa\u8bae\u4f7f\u7528while\u5faa\u73af\u3002<\/td>\n \u5982\u679c\u8fed\u4ee3\u6b21\u6570\u4e0d\u56fa\u5b9a\u4e14\u5fc5\u987b\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u5faa\u73af\uff0c\u5219\u5efa\u8bae\u4f7f\u7528do-while\u5faa\u73af\u3002<\/td>\n<\/tr>\n \n for(init; condition; incr\/decr){
\/\/\u8981\u6267\u884c\u7684\u4ee3\u7801
} <\/span> <\/td>\n while(condition){
\/\/\u8981\u6267\u884c\u7684\u4ee3\u7801
}
<\/span> <\/td>\n do{
\/\/\u8981\u6267\u884c\u7684\u4ee3\u7801
} while(condition)\uff1b
<\/span> <\/td>\n<\/tr>\n\n for(int i = 1; i<= 10; i ++){
System.out.println<\/span><\/span>(i);
}
<\/span> <\/td>\n int i = 1;
while(i<=10){
System.out.println<\/span><\/span>(i);
i++;
}
<\/span> <\/td>\n int i = 1;
do{
System.out.println<\/span><\/span>(i);
i++;
} while(i<= 10);
<\/span> <\/td>\n<\/tr>\n\n for(;;){
\/\/\u8981\u6267\u884c\u7684\u4ee3\u7801
}
<\/span> <\/td>\n while(true){
\/\/\u8981\u6267\u884c\u7684\u4ee3\u7801
}
<\/span> <\/td>\n do{
\/\/\u8981\u6267\u884c\u7684\u4ee3\u7801
} while(true);
<\/span> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n Java For\u5faa\u73af<\/h2>\n
\n for\u5faa\u73af<\/em>\u7528\u4e8e\u591a\u6b21\u8fed\u4ee3\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u3002\u5982\u679c\u8fed\u4ee3\u6b21\u6570\u662f\u56fa\u5b9a\u7684\uff0c\u5219\u5efa\u8bae\u4f7f\u7528for\u5faa\u73af\u3002\n <\/div>\n
\n foreach <\/span>
\n \u4e0d\u5b9a\u5f0ffor\u5faa\u73af<\/span> <\/p>\n Java Simple For\u5faa\u73af<\/h2>\n
\n \u53d8\u91cf\uff0c\u68c0\u67e5\u6761\u4ef6\u548c\u589e\u91cf\/\u51cf\u91cf\u503c\u3002\u5b83\u7531\u56db\u4e2a\u90e8\u5206\u7ec4\u6210:\n <\/div>\n
\n \u6761\u4ef6<\/strong>: \u8fd9\u662f\u7b2c\u4e8c\u6b21\u6761\u4ef6\uff0c\u6bcf\u6b21\u6267\u884c\u65f6\u90fd\u8981\u6d4b\u8bd5\u5faa\u73af\u6761\u4ef6\u3002\u5b83\u7ee7\u7eed\u6267\u884c\u76f4\u5230\u6761\u4ef6\u4e3a\u5047\u3002\u5b83\u5fc5\u987b\u8fd4\u56de\u5e03\u5c14\u503ctrue\u6216false\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u9009\u6761\u4ef6\u3002<\/span>
\n \u58f0\u660e<\/strong>: \u6bcf\u6b21\u6267\u884c\u5faa\u73af\u8bed\u53e5\uff0c\u76f4\u5230\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u4e3a\u5047\u3002<\/span>
\n \u589e\u52a0\/\u51cf\u5c11<\/strong>: \u5b83\u589e\u52a0\u6216\u51cf\u5c11\u53d8\u91cf\u503c\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u9009\u6761\u4ef6\u3002<\/span> <\/p>\n for(initialization;
condition;
incr\/decr){
\/\/statement or code to be executed}
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/p>\n
public <\/span>class ForExample {
public <\/span>static void <\/span>main(String[] args) {
for(int i=1;i<=10;i++){
System.out.println<\/span><\/span>(i);
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1
2
3
4
5
6
7
8
9
10
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n Java\u5d4c\u5957For\u5faa\u73af<\/h2>\n
public <\/span>class NestedForExample {
public <\/span>static void <\/span>main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
System.out.println<\/span><\/span>(i+\" \"<\/span>+j);
}
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n public <\/span>class PyramidExample {
public <\/span>static void <\/span>main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print<\/span>(\"* \"<\/span>);
}
System.out.println<\/span><\/span>();
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n *
* *
* * *
* * * *
* * * * *
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n public <\/span>class PyramidExample2 {
public <\/span>static void <\/span>main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){
for(int j=term;j>=i;j--){
System.out.print<\/span>(\"* \"<\/span>);
}
System.out.println<\/span><\/span>();
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n * * * * * *
* * * * *
* * * *
* * *
* *
*
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n Java for-each\u5faa\u73af<\/h2>\n
for(Type var:array){
\/\/code to be executed}
<\/span><\/code><\/pre>\n<\/p><\/div>\n public class <\/span> ForEachExample {
public <\/span>static void <\/span>main(String[] args) {
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println<\/span><\/span>(i);
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 12
23
44
56
78
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n\u6807\u4e3aFor\u5faa\u73af\u7684Java <\/h2>\n
labelname:for(initialization;
condition;
incr\/decr){
\/\/code to be executed}
<\/span><\/code><\/pre>\n<\/p><\/div>\n \/\/A Java program to demonstrate the use of labeled for <\/span>looppublic <\/span>class LabeledForExample {
public <\/span>static void <\/span>main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println<\/span><\/span>(i+\" \"<\/span>+j);
}
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1 1
1 2
1 3
2 1
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n break bb; <\/strong>\uff0c\u5b83\u5c06\u4ec5\u4e2d\u65ad\u5185\u90e8\u5faa\u73af\uff0c\u8fd9\u662f\u4efb\u4f55\u5faa\u73af\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002\n <\/div>\n public <\/span>class LabeledForExample2 {
public <\/span>static void <\/span>main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break bb;
}
System.out.println<\/span><\/span>(i+\" \"<\/span>+j);
}
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1 1
1 2
1 3
2 1
3 1
3 2
3 3
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n Java\u4e0d\u5b9a\u5f0fFor\u5faa\u73af<\/h2>\n
for(;;){
\/\/code to be executed}
<\/span><\/code><\/pre>\n<\/p><\/div>\n public <\/span>class ForExample {
public <\/span>static void <\/span>main(String[] args) {
for(;;){
System.out.println<\/span><\/span>(\"infinitive loop\"<\/span>);
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java for\u5faa\u73afzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-706","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/706"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=706"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/706\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}