當前位置:首頁 » 編程語言 » 循環java

循環java

發布時間: 2022-01-09 16:20:00

java的for循環如何使用i--;呢

for(inti=10;i>0;i--){
System.out.println("i="+i);
}

上面是個簡單的例子,

每次判斷 i 是否大於0. 如果大於就執行循環語句,執行完後 i的值就自動減一(i--)

當 i 的值減到0時, i > 0 為假 , 循環結束

㈡ java for循環問題

for執行的順序我就不多解釋了。簡單的帶過
進入for循環的時候首先執行的是foo('A')這里輸出A
其次執行foo(『B』)輸出B返回為真,判斷I是不是小於2(i=0小於2)為真
執行i++;
執行foo(『D』);輸出D;
執行foo('C');輸出C
第一次循環結束,開始第二次循環
第二次循環就不再執行foo(『A』);
沖foo(『B』)開始,輸出B返回真,判斷I是不是小於2(i=1小於2)為真
執行i++;
執行foo(『D』);輸出D
執行foo(『C』)輸出C
第二次循環結束
第三次開始
執行foo(『B』)輸出B返回真判斷I是不是小於2(i=2不於2)為假循環結束
所以輸出為ABDCBDCB

㈢ java循環輸出100次java

方法一:while循環
public class Test{
public static void main(String[] args){
int i=1;
while(i<101){ //循環100次
System.out.print("java:"+i+"次"); //每次循環列印一次java
System.out.println(); //列印空行
i++;
}
}

方法二:for循環
public class Test{
public static void main(String[] args){
for (int i=0; i<100; i++ ){ //循環100次
System.out.print("java:"+(i+1)+"次"); //每次循環列印一次java
}
System.out.println(); //列印空行
}
}

㈣ Java循環執行一個方法

你想死循環嗎?在你以下方法中添加:
public static void main(String[] args){

//……
Test test = new Test();//記住類名要大寫,你怎麼寫個小寫呢!!!
while(true){//這是個死循環,自己編寫代碼,實現循環條件!
test.xunhuan(); //你的是靜態方法:也可以直接 Test.xunhuan();

}

}
注意:不知道你循環這個方法干什麼用,好像沒有什麼意義,當按鈕觸發時實現continue的效果
←這句話不懂你要幹嘛,需要的話,能否把你的意圖,具體是要實現什麼說明一下?

㈤ 什麼是循環Java中有哪些循環

循環就是反復的執行同一件事情;
比如:求1到100的累加和就是反復執行 sum =sum +i ; i =i +1;
java循環有三種:
1、do while 循環 2、while 循環 3、for 循環

1、do while 循環 至少執行一次
語法格式:
do {
至少執行一次循環體
sum =sum +i ;//累加自然整數i到sum
i =i +1;//自然整數加一到下一個整數
}while(i<=100);
2、while 循環
語法格式:

while(i<=100);{
//可能一次都不執行
sum =sum +i ;//累加自然整數i到sum
i =i +1;//自然整數加一到下一個整數
}
3、for 循環
語法格式:
for(int i=1;i<=100 ;i++){
//可能一次都不執行
sum =sum +i ;//累加自然整數i到sum
i =i +1;//自然整數加一到下一個整數
}
你明白了嗎?

㈥ java中有什麼循環嗎

while 循環while是最基本的循環,它的結構為:while( 布爾表達式 )

㈦ java幾個簡單的循環

給你簡單例舉java幾個簡單的循環:

  1. 增強for循環

java5引入了主要用於數組的增強型for循環。






㈧ Java的循環有幾種,都有什麼作用

for循環
多用於在知道循環次數的情況下使用!
while循環
多用於在不知道循環次數,需要通過判斷某些條件來決定是否循環的時候使用!

㈨ java的for循環是什麼!

int highGrade=grade[0][0]; // 取grad二位數字的「原點值」
for(int row=0;row<grades.lenght;row++){ //一次循環 獲取二維數組外外層數組個數
for(int column=0;column<grades[row].lenght;column++){//二次循環
grades[row].lenght // 獲取二維 當前外層下 內層個數
if(grades[row][column]<highGrades) // 比較
highGrades=grades[row][column]; 賦值 找到最大的那個
}

你得弄清楚什麼是二維數組 就好明白了

㈩ java的幾種循環有啥區別

do-while 和 while 循環非常相似,區別在於表達式的值是在每次循環結束時檢查而不是開始時。和正規的 while 循環主要的區別是 do-while 的循環語句保證會執行一次(表達式的真值在每次循環結束後檢查),然而在正規的 while 循環中就不一定了(表達式真值在循環開始時檢查,如果一開始就為 FALSE 則整個循環立即終止)。

do-while 循環只有一種語法:

<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>

以上循環將正好運行一次,因為經過第一次循環後,當檢查表達式的真值時,其值為 FALSE($i 不大於 0)而導致循環終止。

資深的 C 語言用戶可能熟悉另一種不同的 do-while 循環用法,把語句放在 do-while(0) 之中,在循環內部用 break 語句來結束執行循環。以下代碼片段示範了此方法:

<?php
do {
if ($i < 5) {
echo "i is not big enough";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
echo "i is ok";

/* process i */

} while(0);
?>

如果還不能立刻理解也不用擔心。即使不用此「特性」也照樣可以寫出強大的代碼來。

add a note User Contributed Notes
do-while
rlynch at lynchmarks dot com
09-May-2006 05:02
I would not be as pessimistic about the potential uses of the do-while construction, myself. It is just about the only elegant way of coding blocks that must have at least a single pass of execution, and where various assessments can short-circuit the logic, falling through.

Consider the example of prompting a user to input a value for a variable. The code in while() format might be:

<?php
$result = null ;
while( $result === null )
{
print "Gimme some skin, bro!> ";
$result = trim( fgets( $stdin ) );
if( $result === '' )
$result = null ;
}
?>

Well, that works. Lets try it in do-while format:

<?php
$result = '';
do {
print "Gimme some skin, bro!> ";
$result = trim( fgets( $stdin ) );
}
while( $result === '' );
?>

See how it just "reads better"? You say to yourself, "set up $result, print a message, get a response, and continue do to that while the nincompoop fails to enter anything."

Likewise, that "Advanced C" programming example turns out to be the elegant "no goto" way to run through exceptionally involved pieces of block-logic, with a trivial 'break' able to get one all the way past all the unnecessary code in one fell swoop.

For just about every other 'while()' block application, it shouldn't be used. While() is what we expect, and while() is what we should use ... except when the surprisingly common "but we have to do it at <i>least once</i>" criterion pops up its pretty head.

rlynch AT lynchmarks DOT com

熱點內容
海康威視存儲卡質量如何 發布:2024-09-19 08:55:35 瀏覽:939
python3默認安裝路徑 發布:2024-09-19 08:50:22 瀏覽:515
環衛視頻拍攝腳本 發布:2024-09-19 08:35:44 瀏覽:417
sqlserveronlinux 發布:2024-09-19 08:16:54 瀏覽:256
編程常數 發布:2024-09-19 08:06:36 瀏覽:952
甘肅高性能邊緣計算伺服器雲空間 發布:2024-09-19 08:06:26 瀏覽:162
win7家庭版ftp 發布:2024-09-19 07:59:06 瀏覽:717
資料庫的優化都有哪些方法 發布:2024-09-19 07:44:43 瀏覽:269
知乎華為編譯器有用嗎 發布:2024-09-19 07:32:20 瀏覽:618
訪問虛擬機磁碟 發布:2024-09-19 07:28:13 瀏覽:670