猜数字








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