C#1119记录 类 string.Split type.TryParse(String,out type 变量)

猜数字








csharp 复制代码
string[] strings = input.Split(",");//?还可以将一串用分隔符号连接字符串转换为数组
if (strings.Length != 2 || !int.TryParse(strings[0], out
    int min) || !int.TryParse(strings[1], out int max)) //?显式类型转换 type.TryParse(string,out type 变量) 这种转换,如果不成功也不会异常
{
csharp 复制代码
 string[] strings = input.Split(",");//?还可以将一串用分隔符号连接字符串转换为数组
 if (strings.Length != 2 || !int.TryParse(strings[0], out
     int min) || !int.TryParse(strings[1], out int max)) //?显式类型转换 type.TryParse(string,out type 变量) 这种转换,如果不成功也不会异常
 {//!int.TryParse()- 如果转换失败返回true
     //3344!=2
     Console.WriteLine("not num,program end");
 }
csharp 复制代码
if (strings.Length != 2 || !int.TryParse(strings[0], out int min) || !int.TryParse(strings[1], out int max))
{
    // 进入这个if块的条件:
    // 1. 分割后不是2个部分 OR
    // 2. 第一个字符串不能转换为整数 OR  
    // 3. 第二个字符串不能转换为整数
}
csharp 复制代码
string input = "abc,50";  // 示例输入
string[] strings = input.Split(",");  // strings = ["abc", "50"]

// 条件判断:
strings.Length != 2       // false (因为有2个部分)
!int.TryParse("abc", out int min)  // true (因为"abc"不能转成整数)
!int.TryParse("50", out int max)   // false (因为"50"能转成整数)

// 最终条件:false || true || false = true
// 所以会进入if块
csharp 复制代码
  input = Console.ReadLine();
  if (!int.TryParse(input, out guess))
  {//字符串不能转换为int guess
      //为什么字符串不能转换为int
      Console.WriteLine("pl input agein,the synax of the num is wrong");
      continue;//?
  }

你的理解完全正确!让我详细解释这段代码:

代码逻辑分析

csharpcsharp复制input = Console.ReadLine();

if (!int.TryParse(input, out guess))

{

// 字符串不能转换为int guess

Console.WriteLine("pl input again, the syntax of the num is wrong");

continue; // 跳过当前循环的剩余部分,重新开始循环

}

为什么字符串不能转换为int?

常见原因:

包含非数字字符:"abc", "12a3", "hello123"

格式不正确:"12.3"(包含小数点),"1,000"(包含逗号)

超出int范围:"9999999999"(int最大约21亿)

空字符串或空格:"", " "

特殊符号:"$100", "50%"

csharp 复制代码
namespace Game
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(">>name<<");
            Console.WriteLine("Hello, World!");
            Console.WriteLine("");

            Console.Write("input the biggest num" +
                "and the smallest num that you think,(like 2,992) : ");
            string input = Console.ReadLine();
            Console.WriteLine("");

            if (string.IsNullOrEmpty(input)) {
                Console.WriteLine("failed");
            }
            else {
                string[] strings = input.Split(",");//?还可以将一串用分隔符号连接字符串转换为数组
                //string input = "abc,50";  // 示例输入
               // string[] strings = input.Split(",");  // strings = ["abc", "50"]

                if (strings.Length != 2 || !int.TryParse(strings[0], out
                    int min) || !int.TryParse(strings[1], out int max)) //?显式类型转换 type.TryParse(string,out type 变量) 这种转换,如果不成功也不会异常
                {//!int.TryParse()- 如果转换失败返回true
                 //3344!=2
                 //if (strings.Length != 2 || !int.TryParse(strings[0], out int min) || !int.TryParse(strings[1], out int max))
                    {
                        // 进入这个if块的条件:
                        // 1. 分割后不是2个部分 OR
                        // 2. 第一个字符串不能转换为整数 OR  
                        // 3. 第二个字符串不能转换为整数
                    }
                    Console.WriteLine("not num,program end");
                }

                else {
                    int target = new Random().Next(min, max);//
                    int guess = 0;

                    do
                    {
                        Console.Write($">>[G]pl input a num between {min} and {max}: ");
                        input = Console.ReadLine();
                        if (!int.TryParse(input, out guess))
                        {//字符串不能转换为int guess
                         //为什么字符串不能转换为int
                         //为什么字符串不能转换为int?
                          //  常见原因:
                            //包含非数字字符:"abc", "12a3", "hello123"
                            //格式不正确:"12.3"(包含小数点),"1,000"(包含逗号)
                            //超出int范围:"9999999999"(int最大约21亿)
                            //空字符串或空格:"", "   "
                            //特殊符号:"$100", "50%"
                            Console.WriteLine("pl input agein,the synax of the num is wrong");
                            continue;//?跳过当前循环的剩余代码
                            //直接开始下一次循环
                        }
                        if (guess < min || guess > max)
                        {
                            Console.WriteLine("the num is out of command,pl input again");
                            continue;
                            //
                        }
                        if (guess != target)
                        {
                            if (guess < target)
                                Console.WriteLine("pl guess bigger ^=^");
                            if (guess > target)
                            
                                Console.WriteLine("pl gusee smaller '>'");
                            }
                            else
                            {
                                Console.WriteLine("Gratulations! y guess right");
                            }
                        } while (guess != target) ;
                    }
             }      
            }
        }
        }
     

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_Up
{
    internal class Course
    {
        private int id;
        public int CourseId
            {
            get { return id; }
            set { id = value; }
        }
        
        public string CourseName { get; set; }

        public void ShowCourse()
        {
            Console.WriteLine("courseid is:"+CourseId+",CourseName is:"+CourseName);
        }
    }
}



csharp 复制代码
namespace C_Up
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Course course = new Course();
            Course yuwen;
            course.CourseId = 2;
            // yuwen.CourseName = string;
            course.CourseName = "math";
            course.ShowCourse();

            Course cs
                =new Course();
            cs.CourseId = 3;
            cs.CourseName = "computer science";
            cs.ShowCourse();


        }
    }
}
相关推荐
玩泥巴的5 小时前
存储那么贵,何不白嫖飞书云文件空间
c#·.net·二次开发·飞书
xiangpanf7 小时前
Laravel 10.x重磅升级:五大核心特性解析
android
robotx10 小时前
安卓线程相关
android
消失的旧时光-194310 小时前
Android 面试高频:JSON 文件、大数据存储与断电安全(从原理到工程实践)
android·面试·json
dalancon11 小时前
VSYNC 信号流程分析 (Android 14)
android
dalancon11 小时前
VSYNC 信号完整流程2
android
dalancon11 小时前
SurfaceFlinger 上帧后 releaseBuffer 完整流程分析
android
用户693717500138412 小时前
不卷AI速度,我卷自己的从容——北京程序员手记
android·前端·人工智能
程序员Android13 小时前
Android 刷新一帧流程trace拆解
android
墨狂之逸才13 小时前
解决 Android/Gradle 编译报错:Comparison method violates its general contract!
android