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();


        }
    }
}
相关推荐
用户69371750013842 小时前
4.Kotlin 流程控制:强大的 when 表达式:取代 Switch
android·后端·kotlin
用户69371750013842 小时前
5.Kotlin 流程控制:循环的艺术:for 循环与区间 (Range)
android·后端·kotlin
Android系统攻城狮3 小时前
Android ALSA驱动进阶之获取周期帧数snd_pcm_lib_period_frames:用法实例(九十五)
android·pcm·android内核·音频进阶·周期帧数
小熊熊知识库5 小时前
C#接入AI操作步骤详解(deepseek接入)
人工智能·flask·c#
雨白5 小时前
Jetpack Compose 实战:自定义自适应分段按钮 (Segmented Button)
android·android jetpack
玖笙&5 小时前
✨WPF编程进阶【7.3】集成动画(附源码)
c++·c#·wpf·visual studio
AskHarries5 小时前
RevenueCat 接入 Google Play 订阅全流程详解(2025 最新)
android·flutter·google
The best are water5 小时前
MySQL FEDERATED引擎跨服务器数据同步完整方案
android·服务器·mysql
消失的旧时光-19436 小时前
我如何理解 Flutter 本质
android·前端·flutter
yue0087 小时前
C# 窗体渐变色
开发语言·javascript·c#