Hiển thị các bài đăng có nhãn Lập trình hướng đối tượng. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Lập trình hướng đối tượng. Hiển thị tất cả bài đăng
Dùng if và goto để tạo vòng lặp

Dùng if và goto để tạo vòng lặp


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

namespace lenh_goto
{
    class 
Program
    
{
        static 
void Main(string[] args)
        {
            
int i=1;
            
lap:
            if (
<= 10)
            {
                
Console.Write(" ");
                
i++;
           
             }
            
goto lap;

        }
    }
}  

 Tạo menu cấp 1 đơn giản, cho phép dùng phím mũi tên lên xuống để di chuyển vị trí chọn

Tạo menu cấp 1 đơn giản, cho phép dùng phím mũi tên lên xuống để di chuyển vị trí chọn


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

namespace Bai16
{
    class 
Program
    
{
        static 
void XuatMenu(int chon)
        {
            
string[] str = {"Chuc nang 1" "Chuc nang 2""Chuc nang 3""Chuc nang 4""Thoat"};
            
int x 20;
            
int[] = {5791113};
            
Console.CursorLeft x;
            
Console.CursorTop 3;
            
Console.ForegroundColor ConsoleColor.White;
            
Console.BackgroundColor ConsoleColor.Black;
            
Console.Write("BAI TAP MENU");

            
Console.ForegroundColor ConsoleColor.Red;
            for (
int i 0str.Lengthi++)
            {
                if (
== chon)
                    
Console.BackgroundColor ConsoleColor.Cyan;
                else
                    
Console.BackgroundColor ConsoleColor.Green;
                
Console.CursorLeft x;
                
Console.CursorTop y[i];
                
Console.Write(str[i]);
            }

        }

        static 
void XuLy()
        {
            
int i 0;
            
ConsoleKeyInfo key;
            while (
true)
            {
                
XuatMenu(i);
                
Console.CursorLeft 40;
                
Console.CursorTop =  18;
                
key Console.ReadKey();
                if (
key.Key == ConsoleKey.DownArrow && 4)
                    
i++;
                if (
key.Key == ConsoleKey.UpArrow && 0)
                    
i--;
                if (
key.Key == ConsoleKey.Enter && == 4)
                    break;
             
                else if(
key.Key == ConsoleKey.Enter)
                    
Console.Write("Ban dang chon chuc nang " +(i+1).ToString());
            }

        }
        static 
void Main(string[] args)
        {
            
XuLy();
        }  

Liệt kê n số nguyên tố đầu tiên

Liệt kê n số nguyên tố đầu tiên


using System;
using System.Collections.Generic;
using System.Linqusing System.Text;
namespace Bai_Tap_13 //Liệt kê n số nguyên tố đầu tiên  
{
    class 
Program
    
{
        static 
bool KTSNT(int n)
        {
            if (
== || == || n==3)
                return 
true;
            for (
int i 2<= 2i++)
                if (
== 0)
                    return 
false;
            return 
true;
        }
        static 
void Output(ref int n)
        {
            
int dem=0;
            for (
int i 1dem != ni++)
            {
                if (
KTSNT(i) == true)
                {
                    
Console.Write("\t");
                    
dem++;
                }
            }
        }
        static 
void Main(string[] args)
        {
            
int luachonn;
            do
            {
                
Console.Write("\nNhap vao so n = ");
                
int.Parse(Console.ReadLine());
                
Console.Write("\n" " so nguyen to dau tien la: \n\n");
                
Output(ref n);
                
Console.Write("\n\nContinue? (1:Exit) => ");
                
luachon int.Parse(Console.ReadLine());
            } while (
luachon != 1);
            
Console.Write("\n");
        }
    }
}  
Ứng dụng lớp điểm ký tự viết trò chơi rắn săn mồi

Ứng dụng lớp điểm ký tự viết trò chơi rắn săn mồi


CDIEM.cs
PHP Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace BT19_ransanmoidos
{
    class 
CDIEM
    
{
        public 
int x;
        public 
int y;
        public 
char c;

        public 
CDIEM()
        {
        }

        public 
CDIEM(int xxint yychar cc)
        {
            
xx;
            
yy;
            
cc;
        }

        public 
bool KiemTraX(int xx)
        {
            if (
xx <= || xx >= Console.WindowWidth)
            {
                return 
false;
            }
            return 
true;
        }
        public 
bool KiemTraY(int yy)
        {
            if (
yy <= || yy >= Console.WindowHeight)
            {
                return 
false;
            }
            return 
true;
        }

        public 
void DichPhai(int k)
        {
            
+= k;
        }
        public 
void DichTrai(int k)
        {
            
-= k;
        }
        public 
void DichLen(int k)
        {
            
-= k;
        }
        public 
void DichXuong(int k)
        {
            
+= k;
        }

        public 
void Xuat()
        {
            
Console.SetCursorPosition(xy);
            
Console.Write(c);
        }

        public 
void Xoa()
        {
            
Console.Clear();
        }

        public 
float KhoangCach(CDIEM M)
        {
            return 
0;
        }
        public 
int KhoangCachX()
        {
            return 
x;
        }
        public 
int KhoangCachY()
        {
            return 
y;
        }

    }
}  

Program.cs
PHP Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;

namespace BT19_ransanmoidos
{
    class 
Program
    
{
        static 
ArrayList list = new ArrayList();
        static 
CDIEM thucan;
        static 
Random rnd = new Random();
        static 
int TOCDO 100;
        static 
String huong "Phai";
        
// ký tự đại diện cho rẳn
        
const char KYTU '0';

        static 
void Main(string[] args)
        {
            
Console.CursorVisible false;

            list.
Add(new CDIEM(00KYTU));
            
TaoThucAn();

            
RanChay();

            
Console.Clear();
            
Console.SetCursorPosition(00);
            
Console.ForegroundColor ConsoleColor.Yellow;
            
Console.WriteLine("YOU LOSE!!!");
            
Console.WriteLine("Press Any Key To Exit!!!");
            
Console.Read();
        }

        static 
void RanChay()
        {
            do
            {
                
NhanPhimBam();

                
// Lấy đoạn cuối trong rắn
                
CDIEM doanCuoi = (CDIEM)list[list.Count 1];
                
CDIEM c = new CDIEM(doanCuoi.xdoanCuoi.yKYTU);

                
// Thực hiện dịch chuyển rắn trong đoạn cuối.
                
switch (huong)
                {
                    case 
"Len":
                        
c.DichLen(1);
                        break;
                    case 
"Xuong":
                        
c.DichXuong(1);
                        break;
                    case 
"Trai":
                        
c.DichTrai(1);
                        break;
                    case 
"Phai":
                        
c.DichPhai(1);
                        break;
                }
                if (
TimXTrongList(c))
                {
                    break;
                }
                
// Thêm một đoạn vào list.
                
list.Add(c);
                
// Remove phần tử đầu tiên khỏi  list.
                
list.RemoveAt(0);

                
// kiểm tra có thức ăn chưa.
                
KiemTraThucAn();

                try
                {
                    
VeLai();
                }
                catch (
Exception)
                { }

                
Thread.Sleep(TOCDO);

            } while (
KiemTraTiepTuc());
        }

        static 
void VeLai()
        {
            
Console.Clear();

            
// Vẽ rắn.
            
foreach (CDIEM c in list)
            {
                
c.Xuat();
            }

            
// Ve thuc an
            
thucan.Xuat();
        }

        static 
void TaoThucAn()
        {
            
int hang rnd.Next(1Console.WindowWidth);
            
int cot rnd.Next(1Console.WindowHeight);

            
thucan = new CDIEM(hangcot'G');
            
//thucan = new CDIEM(5, 5, 'G');
        
}

        static 
void KiemTraThucAn()
        {
            
CDIEM c = (CDIEM)list[list.Count 1];

            if (
c.== thucan.&& c.== thucan.y)
            {
                
CDIEM cDau = (CDIEM)list[0];
                
CDIEM cMoi = new CDIEM(cDau.xcDau.yKYTU);
                list.
Insert(0cMoi);
                
TaoThucAn();
            }
        }

        static 
bool KiemTraTiepTuc()
        {
            
CDIEM c = (CDIEM)list[list.Count 1];
            if (
c.|| c.>= Console.WindowHeight ||
                
c.|| c.>= Console.WindowWidth 1)
            {
                return 
false;
            }
            return 
true;
        }

        static 
bool TimXTrongList(CDIEM x)
        {
            foreach (
CDIEM c in list)
            {
                if (
c.== x.&& c.== x.y)
                {
                    return 
true;
                }
            }
            return 
false;
        }

        static 
void NhanPhimBam()
        {
            while (
Console.KeyAvailable)
            {
                switch (
Console.ReadKey(true).Key)
                {
                    case 
ConsoleKey.UpArrow:
                        
huong "Len";
                        break;
                    case 
ConsoleKey.DownArrow:
                        
huong "Xuong";
                        break;
                    case 
ConsoleKey.LeftArrow:
                        
huong "Trai";
                        break;
                    case 
ConsoleKey.RightArrow:
                        
huong "Phai";
                        break;
                }
            }
        }

    }
}  

Đây là bài viết mình sưu tằm, có  vần đề gì các bạn thông cảm nhé