Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Giải giúp em bài tập C# với ạ

03:16 14-05-2022 865 lượt xem 3 bình luận

Đề: Máy tính ra 1 số bất kỳ trong đoạn [0…100], người chơi sẽ tiến hành đoán số máy ra, chỉ được phép đoán sai tối đa 7 lần. Mỗi lần đoán sai thì phải hiện thông báo rút gọn đoạn cho người chơi dựa trên câu trả lời trước đó.

vd : kq = 60.

lần 1 : TL = 50. Hiện thông báo đoạn [50..100] 

lần 2 : TL = 70 Hiện thông báo đoạn [50..70] 

lần 3 : TL = 55 Hiện thông báo đoạn [55..70] 

............

Random rd = new Random();
int x = rd.Next(101);
Console.WriteLine("{0}",x);
Console.WriteLine("Hay doan so tu 1 toi 100");
Console.Write("moi ban nhap so ban chon: ");
int i = 0;

while (true)
{
    Console.Write("moi ban nhap so ban chon: ");
    i++;
    int a = int.Parse(Console.ReadLine());
    string ketqua = x < a ? "so ban doan lon hon ket qua" : "so ban doan nho hon ket qua";
    Console.WriteLine("{0}", i);

    if (a == x && i <=7)
    {
        Console.WriteLine("ban da dung");
        break;
    }
    else
    {
        if (i == 7)
        {
            Console.WriteLine("ban da thua");
            break;
        }
        else
        {

            if (a < x)
            {             

                Console.WriteLine("so ban chon tu {0}:{1}", a, 100);              
                b += a;
            }
            else
                
            Console.WriteLine("so ban chon tu {0}:{1}", b, a);
            ;
        }
            Console.WriteLine("{0}",ketqua);
    };

Em làm tới đây thì bí, mong các anh giúp đỡ

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
ptminh.kd@gmail.com đã bình luận 15:16 28-05-2022
// Đúng theo ví dụ của bạn. Các trường hợp khác có thể không đúng.
tuananh511 đã bình luận 21:41 26-05-2022

Hình như cái while không dừng nha

using System;

public class Program
{
    public static void Main()
    {
        Random rd = new Random();
        int x = rd.Next(101);
        Console.WriteLine("{0}", x);
        Console.WriteLine("Hay doan so tu 0 toi 100");
        Console.Write("Moi ban nhap so ban chon: ");
        int i = 0;
        int b = 0;
        int stop=0;
        while (stop<1)
        {
            Console.Write("moi ban nhap so ban chon: ");
            i++;
            int a = int.Parse(Console.ReadLine());
            string ketqua = x < a ? "So ban doan lon hon ket qua" : "So ban doan nho hon ket qua";
            Console.WriteLine("Day la lan doan so {0}", i);

            if (a == x && i <= 7)
            {
                Console.WriteLine("Ban da dung");
                stop=1;
            }
            else
            {
                if (i == 7)
                {
                    Console.WriteLine("Ban da thua");
                    stop = 1;
                }
                else
                {

                    if (a < x)
                    {

                        Console.WriteLine("So ban chon tu {0}:{1}", a, 100);
                        b += a;
                    }
                    else

                        Console.WriteLine("So ban chon tu {0}:{1}", b, a);
                    ;
                }
                Console.WriteLine("{0}", ketqua);
            };
        }
    }
}
 

 

Câu hỏi mới nhất