請問這樣寫有問題么?輸出正確,但有一次警告
static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? //聲明整型數組,保存一組整數
? ? ? ? ? ? int[] num = new int[] { 3,34,43,2,11,19,30,55,20};
? ? ? ? ? ? //請完善代碼,判斷數組中有沒有7的整倍數
? ? ? ? ? ? for(int i = 0 ; i < num.Length ; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(num[i]%7==0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write("有7的整倍數");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write("沒有7的整倍數");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
2019-03-28
首先if中不能放
break; 這個只是終止循環的。 其次,如果循環里套了if,在if中放入break,是跳出循環。 continue?這個是跳出本次循環的。 int[]?num?=?new?int[]?{?3,34,43,2,11,19,30,55,20};? //請完善代碼,判斷數組中有沒有7的整倍數 ?for?(int?i?=?0;?i?<?num.Length;?i++){??? ??Console.WriteLine();//換行 ??????if?(num[i]?%?7?==?0)????{???? ??????????//跳出本次循環前會打印,??????? ???????????Console.Write("有7的整倍數"); ????????????continue;//跳過本次循環 ?????} ?????else????{ ?????????Console.Write("沒有7的整倍數"); ????????continue; ? ????}???????????? ??} ps:另外別人回答你時,你要自己求證下,不要一上來就是質問! 建議遇到問題的解決思路是?上網搜索資料》?代碼證明?》?還是不行?》虛心請教 例如你這個問題?搜索?C#?if?跳出本次循環2019-03-26
else中的break刪除。否則循環一次就直接退出了。
?//聲明整型數組,保存一組整數
? ? ? ? ? ? int[] num = new int[] { 3,34,43,2,11,19,30,55,20};
? ? ? ? ? ? //請完善代碼,判斷數組中有沒有7的整倍數
? ? ? ? ? ? bool y = false;
? ? ? ? ? ? foreach(int n in num){
? ? ? ? ? ? ? ? if(n%7==0){
? ? ? ? ? ? ? ? ? ? y = true;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? if(y){
? ? ? ? ? ? ? ? Console.Write("有7的整數倍");
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? Console.Write("沒有7的整倍數");
? ? ? ? ? ? }