日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java 9.while语句

發(fā)布時(shí)間:2025/3/20 java 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 9.while语句 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

while語句

while語句就是一種循環(huán)語句,像if語句一樣計(jì)算布爾值表達(dá)式的值,并在其值為true時(shí)執(zhí)行一條語句(稱為循環(huán)體).但while會(huì)在循環(huán)體執(zhí)行完畢后再次計(jì)算表達(dá)式的值,這一點(diǎn)與if語句不同。


?

1 import java.text.DecimalFormat; 2 import java.util.Scanner; 3 4 public class Average{ 5 6 public static void main(String[] args){ 7 int sum = 0, value, count = 0; 8 double average; 9 10 Scanner scan = new Scanner(System.in); 11 12 System.out.print("Enter an integer (0 to quit): "); 13 value = scan.nextInt(); 14 15 while (value != 0) { 16 17 count++; 18 19 sum += value; 20 System.out.println("The sum so far is " + sum); 21 22 System.out.print("Enter an integer (0 to quit): "); 23 value = scan.nextInt(); 24 } 25 26 System.out.println (); 27 28 if (count == 0) 29 System.out.println("No values were entered."); 30 else{ 31 average = (double)sum / count; 32 33 DecimalFormat fmt = new DecimalFormat("0.###"); 34 System.out.println("The average is " + fmt.format(average)); 35 } 36 } 37

嵌套循環(huán)

一個(gè)循環(huán)體中可能包含另一個(gè)循環(huán),稱為嵌套循環(huán)。

1 import java.util.Scanner; 2 3 public class PalindromeTester 4 { 5 public static void main(String[] args) 6 { 7 String str, another = "y"; 8 int left, right; 9 10 Scanner scan = new Scanner(System.in); 11 12 while (another.equalsIgnoreCase("y")) 13 { 14 System.out.println("Enter a potential palindrome:"); 15 str = scan.nextLine(); 16 17 left = 0; 18 right = str.length() - 1; 19 20 while (str.charAt(left) == str.charAt(right) && left < right) 21 { 22 left++; 23 right--; 24 } 25 26 System.out.println(); 27 28 if (left < right) 29 System.out.println("That string is NOT a palindrome."); 30 else 31 System.out.println("That string IS a palindrome."); 32 33 System.out.println(); 34 System.out.print("Test another palindrome (y/n)? "); 35 another = scan.nextLine(); 36 } 37 } 38 } 39

?

轉(zhuǎn)載于:https://www.cnblogs.com/H97042/p/10925557.html

總結(jié)

以上是生活随笔為你收集整理的Java 9.while语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。