c#基礎語句——循環語句(for、while、foreach)


循環類型:for、while、foreach

循環四要素:初始條件——>循環條件——>循環體——>狀態改變

1、for

格式:

for(初始條件;循環條件;狀態改變)

{循環體(break;跳出循環體)}ou給出初始條件,先判斷是否滿足循環條件,如果不滿足條件則跳過for語句,如果滿足則進入for語句執行,for語句內的代碼執行完畢后,將按照狀態改變,改變變量,然后id判斷是否符合循環條件,符合則繼續執行for語句內的代碼,直到變量不符合循環條件則終止循環,或者碰到break;命令,直接跳出當前的for循環。

break在這里是跳出循環的意思。

for可以嵌套。

 

1、讓用戶輸入一個100以內的數
打印1-100之間所有的數,用戶輸入的數除外

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 作業題1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("請輸入一個100以內的數:");
            int user = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < 101; i++)
            {
                   if (i != user)
                    Console.WriteLine(i);
            }

            Console.ReadLine();
        }
    }
}

輸出結果:

 

2、讓用戶輸入一個100以內的數
打印1-這個數之間所有的數的和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 作業題2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("請輸入一個100以內的數:");
            int user = Convert.ToInt32(Console.ReadLine());
            int sum = 0;
            for (int i = 0; i < 101; i++)
            {
                sum += i;
                if (i==user)
                {
                    break;
                }
            }
            Console.WriteLine(sum);

                Console.ReadLine();
        }
    }
}

輸出結果:

 



3、打印100以內所有的質數/素數,再求和

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 作業題3
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            for (int i = 2; i <= 100; i++)
            {
                int count = 0;

                for (int j = 1; j <= i; j++)
                {
                    if (i % j == 0)
                        count++;
                }
                if (count == 2)
                {
                    Console.Write(i+",");
                    sum += i;
                }

            }
            Console.WriteLine(sum);

            Console.ReadLine();
        }
    }
}

輸出結果:

 問題分析:變量的定義域搞混亂了。把int count=0;定義在for循環外面了。

4、使用一個for循環,分別打印出來100以內的奇數和偶數,分別求和
奇數:1,3,5,7.....
偶數:2,4,6,8.....
奇數和:xxx
偶數和:xxx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 作業題4
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum1 = 0;
            int sum2 = 0;
            string jishu="" ;
            string oushu="" ;

            for (int i = 0; i < 100; i++)
            {
                if (i % 2 == 1)
                {
                    jishu += i + ",";
                    sum1 += i;
                }
                else 
                {
                    oushu += i + ",";
                    sum2+=i;
                }
            }
            Console.WriteLine("奇數"+jishu);
            Console.WriteLine("偶數" + oushu);
            Console.ReadLine();

 輸出結果:

 

問題分析:沒有想到將jishu定義為string類型進行無限拼接。

5、猜拳(三局兩勝)
請輸入您的手勢:石頭
用戶手勢:石頭 電腦手勢:剪刀
用戶勝:1 電腦勝:0

 

請輸入您的手勢:石頭
用戶手勢:石頭 電腦手勢:石頭
用戶勝:1 電腦勝:0

 

請輸入您的手勢:石頭
用戶手勢:石頭 電腦手勢:包袱
用戶勝:1 電腦勝:1

 

請輸入您的手勢:石頭
用戶手勢:石頭 電腦手勢:剪刀
用戶勝:2 電腦勝:1
用戶勝利!!!

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM