亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求,詳細講解foreach循環的用法,急!

求,詳細講解foreach循環的用法,急!

UYOU 2021-09-08 19:07:58
foreach( 這里的參數具體什么意思??? ){ ...}比如說 我想找一個 字符 在不在一個字符數組 char[] args 里.
查看完整描述

3 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

foreach 語句為數組或對象集合中的每個元素重復一個嵌入語句組。foreach 語句用于循環訪問集合以獲取所需信息,但不應用于更改集合內容以避免產生不可預知的副作用。

能夠應用的編程語言類別:Java、C# 、PHP、D語言(Phobos庫)。

foreach語句是c#中新增的循環語句,他對于處理數組及集合等數據類型特別方便。

foreach語句的一般語法格式如下:

foreach(數據類型 標識符 in 表達式)

{

循環體

}。

C# 示例:

12345int[]arr=newint[]{0,1,2,3,4};foreach(int in arr){Console.Write(i);}

JAVA示例:

1234int[] a = {1,2,3};for(int i : a)System.out.print(i + ",");}


查看完整回答
反對 回復 2021-09-13
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

foreach(char arg in args)//"char"是args里面每個元素的類型,arg就是從args里面提取出的“char”類型的一個元素,in是關鍵字,args就是你要操作的集合類型數據。其實和for()循環類似,只是不需要記錄循環步數,同時,在foreach過程中,args是不允許被改變的。
{
if(arg == ch)
{
//存在
}
else
{
//不存在
}
)

 


查看完整回答
反對 回復 2021-09-13
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

using System;

namespace Example_6
{
/// <summary>
/// 此程序演示如何使用 foreach 循環。
/// </summary>
class DigitLetterPunctuation
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 存放字母的個數
int countLetters = 0;
// 存放數字的個數
int countDigits = 0;
// 存放標點符號的個數
int countPunctuations = 0;
// 用戶提供的輸入
string input;

Console.WriteLine("請輸入一個字符串");
input = Console.ReadLine();

// 聲明 foreach 循環以遍歷
// 輸入的字符串中的每個字符。
foreach(char chr in input)
{
// 檢查字母
if(char.IsLetter(chr))
countLetters++;
// 檢查數字
if(char.IsDigit(chr))
countDigits++;
// 檢查標點符號字符
if(char.IsPunctuation(chr))
countPunctuations++;
}

Console.WriteLine("字母的個數為: {0}", countLetters);
Console.WriteLine("數字的個數為: {0}", countDigits);
Console.WriteLine("標點符號的個數為: {0}", countPunctuations);

}
}
}



查看完整回答
反對 回復 2021-09-13
  • 3 回答
  • 0 關注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號