max)//超過範圍 { Console.WriteLine("輸入錯誤,請重新輸入"); } else { if (guess < answer"> max)//超過範圍 { Console.WriteLine("輸入錯誤,請重新輸入"); } else { if (guess < answer"> max)//超過範圍 { Console.WriteLine("輸入錯誤,請重新輸入"); } else { if (guess < answer">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20240903_終極密碼
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //先隨機生成一個答案,介於1~100之間
            int answer = new Random().Next(0, 101);
            Console.WriteLine("答案是:" + answer);
            Console.WriteLine("遊戲開始,請輸入1~100之間的數字");
            Console.WriteLine("------------------------------------");

            //判定
            int guess = 0;
            int count = 0;
            int min = 0;
            int max = 100;
            while (guess != answer)
            {
                Console.Write("請輸入數值:");
                guess = int.Parse(Console.ReadLine());
                count++;
                if (guess < min || guess > max)//超過範圍
                {
                    Console.WriteLine("輸入錯誤,請重新輸入");
                }
                else
                {
                    if (guess < answer || guess > 100) //小於答案
                    {
                        min = guess;
                        Console.WriteLine($"介於{min}~{max}之間");
                    }
                    else if (guess > answer || guess < 0) //大於答案
                    {
                        max = guess;
                        Console.WriteLine($"介於{min}~{max}之間");
                    }
                    else //正確答案
                    {
                        Console.WriteLine("恭喜答對,總共猜了" + count + "次");
                    }

                }
            }
            
        }
    }
}