Loop
03-2循环案例 (逐字弹出文本)
按住alt框选可以矩形框选
1.Console.Write(); won't return
2.string表示字符串 char表示字符串中的一个字符
3.use ' ' for character " " for string
4.' ' character literal can only have one character (for example '\n')
meanwhile, " " can have many characters
5. \n means new line character, is also one character
6.循环体只有一条语句不用加{} (如图高亮语句)
for (int k = 0; k < 10000 * 10000; k++) ;
//for循环后面是空语句用分号直接结束,否则用花括号不加分号
03-3二重循环
\t 制表符(Tab 或者 Tabulator Character)是一种控制字符,用于在文本中插入水平间距
for (int i =0; i <= 9; i++)//i代表行
{
for (int j = 0;j <= i; j++)//j代表列
{
Console.Write("{0}*{1}={2}\t",j,i,j*i);
}
Console.WriteLine();
}
03-4 break_continue
break;//跳出循环(跳出break所在的那层for循环)
continue;//结束本次循环,但不退出循环,而是开始下一次的循环(今天不活了,明天继续)
03-5交互案例(hp交互掉血)
1.列行
Console.SetWindowSize(100, 20); //(列,行)
2.Console.ReadLine();
也可以是等待用户输入回车键的一个交互 (已读)
pwd = Console.ReadLine();
用于从控制台读取用户输入的一行文本,并将其赋值给变量 pwd
3.Console.Write
();
用于输出文本,但不会自动添加换行符。
4.初始化的hp值不能放在for循环里面
正确做法
错误做法
03-6 Blackjack
1.rnd
Console.WriteLine("Your current point total is {0}\n Hit?", player);
03-07 last digits lottery
1.Next方法
int lastDigits = rnd.Next(0,10);//定义变量(前半段还没有被创建出来),并分配空间
2.自制彩票机
using System;
class Program
{
static void Main(string[] args)
{
#region last digits lottery
Random rnd = new Random();
int luckyNumber = rnd.Next(0,10);
int money = 5000;
Console.WriteLine("the lucky number is {0}", luckyNumber);
for (; ;)
{
Console.WriteLine("your current money is {0}", money);
Console.WriteLine("press enter to get a lottery ball");
Console.ReadLine();
Console.WriteLine("-50!");
money -= 50;
int ballNumber = rnd.Next(100, 1000);
Console.WriteLine("the lucky ball is{0}",ballNumber);
if (ballNumber %10 ==luckyNumber)
{
Console.WriteLine("you win!\n +400");//win 400
money += 400;
}
else
{
Console.WriteLine("Better luck next time.");
}
Console.WriteLine("press n to quit, any other key to continue");
string s = Console.ReadLine();
if (s == "n")
{
break;
}
else
{
continue;
}
}
Console.ReadLine();
#endregion
}
}
Array
1.定义
由简单数字构成的数据结合,如背包物品集合(arrays),技能集合
2.语句
int[]a;//定义数组 (还未被创建)
a=new int[5];//分配数组空间, 创建一个长度为 5 的整数数组。
[]里可以不写:a=int[5]{2,4,6,8,10}----------a=int[]{2,4,6,8,10}
a.Length访问长度
3.数组与内存
1)内存单位
1Byte = 8 bit (一字节为八位) B/字节/Byte
1 int= 4byte (一个整数占4个字节)
2)Script
int[] a = new int[5] { 1, 2, 3, 4, 5 };
int[] b = a;
b[0]++;
i) 在像C和C++这样的低级编程语言中,数组变量存储的是数组在内存中的首元素的地址,即数组的起始地址。这意味着数组名可以用作指向数组第一个元素的指针。
图中存储的是这20B在内存中的起始地址。(如1001)
ii)将数组a
赋值给数组b
时,b
将引用同一个数组对象,而不是创建一个数组的副本
iii) 32位二进制数 可以表示32位系统中任何有效内存地址
无符号32位整数(unsigned int),因为它的范围从0到4,294,967,295,正好覆盖了所有可能的内存地址。
在一个32位系统中,内存地址(也称为指针)是用32位的数字表示的。每一个内存地址都是一个32位(0或1)的数值,它指向内存中的一个字节。32位表示可以表示的最大数字是2^32 - 1,也就是4,294,967,295。这意味着32位系统能够寻址的最大内存空间是4GB(因为每个地址指向一个字节,总共有4GB的地址空间)。
3)32位系统和64位系统
32位系统和64位系统主要指计算机处理器和操作系统的数据处理能力和内存寻址能力。
2^32B=4GB
2^64B= 2^34 GB。
4)练习
i)输入输出数组
int[] a = new int[n];//声明一个名为a的整数数组,并初始化这个数组,使其可以容纳n个整数。
代码 Console.WriteLine(a);
在C#中,直接打印一个数组对象时,只会输出其类型信息(例如System.Int32[],System.Int32[]
是 C# 中数组类型的完整类型名称。它表示一个由 System.Int32
类型(即 int
)元素组成的数组。),而不是数组的元素内容。如果想打印内容,需要遍历数组来格式化数组的输出。如下:
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("Element {0}: {1}", i, a[i]);
}
ii)统计数组内容
char
是表示单个字符的基本数据类型。它是 System.Char
类型的别名。char
类型用于存储一个 16 位的 Unicode 字符。char[]== string ,string可以当作是一组char(不全相同)
空格键也算是特殊字符。在编程和字符处理的上下文中,空格(
)是一个有效的字符,尽管它看起来是空白的。它在许多情况下都被视为特殊字符,原因如下:
、1. 空格的字符编码 在ASCII编码中,空格字符的值是32(十进制)或0x20(十六进制)。 在Unicode中,空格也有相同的编码,表示为空白字符。
2. 编程中的空格 空格字符:在字符串处理中,空格是一个有效的字符,可以与其他字符一样处理。 字符串中的空格:字符串可以包含空格,这些空格在字符串的比较、分割、拼接等操作中都会被考虑。
iii)对话字幕
1.使用 new string[] { ... }
来初始化字符串数组。
2.dialogs[i][j] //表示第i行的第j个字符
4.二维数组
1)表达
二维数组中的一行是一个一维数组
[1,2] //[行, 列]
int[] a = new int[] { 1, 2, 3 };
int[,] b = new int[5, 3] {
{ 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 }, { 4, 5, 6 }, { 5, 6, 7 }
};
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
Console.Write("{0}\t", b[i, j]);
};
Console.WriteLine();
}
2)语法
b.Length
属性 b.GetLength()方法
例如 int[,] b = new int[3, 4];
b.Length
的结果是 12,因为这个数组包含 3 * 4 = 12 个元素。
如果你想分别获取数组的行数和列数,可以使用 GetLength
方法:
b.GetLength(0)
获取数组的行数。// b = new int[3, 4];//3的index是0,4的index是1。b.GetLength(0)会得到行数3.
b.GetLength(1)
获取数组的列数。
3)输出
一维数组中,一个元素是string的话,string可以看作也是一个数组,里面的char是数组里的元素。这时候遍历一个char是使用b[i][j]输出。
二维数组中 遍历元素是用b[i,j]输出。
4)制表符 \t
Console.Write("{0}\t",b[i,j]);
\t是制表位,会自动对齐。要放在“”中 不然会报错。
5.冒泡排序和选择排序
- 冒泡排序:Bubble Sort (在每次迭代中,冒泡排序通过相邻元素的比较和交换,将较大的元素逐步向右移动,就像气泡上升一样。)
- 选择排序:Selection Sort
冒泡排序
using System;
class Program
{
static void Main(string[] args)
{
#region 选出一个数
//int[] a =new int[] { 1, 2, 3 };
//Console.WriteLine("pls type in a number: ");
//int n = int.Parse(Console.ReadLine());
//bool found = false;
//for (int i = 0; i < a.Length; i++)
//{
// if (a[i] == n)
// {
// Console.WriteLine(i);
// found = true;
// }
//}
//if (!found)
//{
// Console.WriteLine("no element");
//}
//Console.ReadLine();
#endregion
#region 冒泡排序练习,用冒泡排序找五个数的最大者,将其调换到数组的最后一位
int[] a = new int[] { 9,8,7,6,5,11,12,12,13 };
for (int j=1; j<a.Length; j++)
{
for (int i = 0; i < a.Length - j; i++)
{
if (a[i] > a[i + 1])
{
int t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
Console.WriteLine("_____________________");
for (int i = 0;i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
#endregion
}
}
选择排序
debug:
- 内层循环的起始条件错误:内层循环
for (int i = 1; i < a.Length; i++)
应该从当前未排序部分的第一个元素开始,而不是总是从1开始。 -
交换元素位置错误:交换最小元素和第一个元素的位置始终是
a[0]
,而不是当前未排序部分的第一个元素位置。
#region 选择排序
int[] a = new int[] { 9, 8, 7, 6, 5, 14, 10, 12, 13,102,0,2,4,8 };
for(int j=1; j<a.Length; j++)
{
int min_index = j-1;
for (int i = j; i < a.Length; i++)
{
if (a[i] < a[min_index])
{
min_index = i;
}
}
Console.WriteLine("minimal number index is a[{0}]\nminimal number is {1}", min_index, a[min_index]);
int t = a[j - 1];
a[j - 1] = a[min_index];
a[min_index] = t;
}
Console.WriteLine("_____________________");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
#endregion
Binary Search
二分搜索必须是经过从小到大排序的
速度快
方法
05.1方法的使用
static 返回值类型 方法名(参数列表)
{
}
1)如果没有返回值(不需要返回结果)就填void
2)方法名(returnType)是自定义的
方法是为了功能复用
string[] args
是C#程序中的一个参数,它表示一个字符串数组,用来接收命令行参数。
让我们详细解释一下:
1. 什么是字符串数组 string[]
?
string[]
表示一个字符串数组,也就是一个可以存储多个字符串的集合。- 在C#中,数组是一种数据结构,用来存储相同类型的多个值。
- 例如,
string[] fruits = new string[] { "apple", "banana", "cherry" };
定义了一个字符串数组,包含了三个字符串:"apple"、"banana" 和 "cherry"。
2. args
参数的作用
args
是字符串数组的名称。你可以将它命名为其他名字,但args
是约定俗成的名字。- 当你运行C#程序时,可以在命令行中传递参数,这些参数会被存储在
args
数组中。 - 例如,如果你在命令行中运行
MyProgram.exe arg1 arg2 arg3
,那么args
数组就会包含这些字符串:args[0]
是 "arg1",args[1]
是 "arg2",args[2]
是 "arg3"。 - 分大小写
- F11(Step Into):逐步执行 且 进入方法内部。 Shift+F11(Step Out)
- F10(Step Over):逐步执行 (但不进入方法内部)。
- step out:退出当前方法并返回调用该方法的上一层。 (会把当前层后面的语句执行完)
F5再ctrl alt C
调用堆栈(Call Stack)是一个数据结构,用于跟踪程序执行过程中方法或函数调用的顺序。它记录了程序当前执行路径上的所有活动方法,帮助开发者理解程序的执行流程和定位错误。
F10、F11下面都会有 call stack窗口。正常运行没有。
3.构建方法说明
///
/// <summary>
/// 计算1~n的和
/// </summary>
/// <param name="n">传入n</param>
/// <returns>1~n的和</returns>
4.方法的用途必须专一
比如上面的求和方法,return完了如果要再输出去static void Main(string[] args)里输出
05.2方法练习
1)报错
报错1 The left hand side must be a variable, property or indexer
if (n % i = 0)
if (n % i == 0)
简直瞎了
报错2 Argument cannot convert from int to char[]
格式化输出的弱智报错
在 C# 中,Console.WriteLine
方法不能直接接受多个参数来输出。需要通过格式化字符串或字符串插值来实现。这是因为 Console.WriteLine
方法的重载没有接受多个普通变量的方法。
报错3 not all code paths return a value
缺少所有路径的返回值
if有返回值 else没有。
if (n == a * c + b * c + c * c)
{
return true;
}
return false;//没有这行会出现如上报错
2)代码 判断素数
using System;
class Program
{
/// <summary>
/// 判断一个数是否为素数
/// </summary>
/// <param name="n">待判断的正整数</param>
/// <returns>是或者不是</returns>
static bool IsPrime(int n)//2 n-1
{
for (int i = 2; i < n; i++) //好像弱智
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
bool b = IsPrime(n);
Console.WriteLine(b);
Console.WriteLine("{0}{1} a prime number",n,b?" is":" is not");
}
}
3)四则运算和基础表达式 以及我是弱智
a^3 就是 a*a*a 立方!!!!
报错4:Operator:'^' cannot be applied to operands of type 'bool' and 'int'
^不能用在运算中 因为这是亦或符
整数除法&&浮点数除法
-
Python:
- 如果使用浮点数(默认的除法运算):
153 / 10
会得到15.3
。 - 如果使用整数除法(使用双斜杠运算符):
153 // 10
会得到15
。
- 如果使用浮点数(默认的除法运算):
-
Java:
- 如果变量是浮点型(如
double
或float
):153 / 10
会得到15.3
。 - 如果变量是整型(如
int
):153 / 10
会得到15
。
- 如果变量是浮点型(如
-
JavaScript:
- 默认除法运算会返回浮点数:
153 / 10
会得到15.3
。
- 默认除法运算会返回浮点数:
-
C/C++:
- 如果变量是浮点型(如
float
或double
):153 / 10
会得到15.3
。 - 如果变量是整型(如
int
):153 / 10
会得到15
。
- 如果变量是浮点型(如
4.Narcissistic number
using System;
class Program
{
/// <summary>
/// 判断一个数是否为素数
/// </summary>
/// <param name="n">待判断的正整数</param>
/// <returns>是或者不是</returns>
static bool IsFlower(int n)
{
int a = n / 100;
int b = n / 10 % 10;//n / 10-a*10;弱智
int c = n % 10;
if (n == a * a*a + b*b*b + c * c * c)
{
return true;
}
return false;
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
bool f = IsFlower(n);
//int n = 153;
//int a = n / 100;
//int b = n / 10 %10;//n / 10-a*10;
//int c = n % 10;
//Console.WriteLine("a: {0}, b: {1}, c: {2}", a, b, c);
Console.WriteLine("{0}{1} a flower number",n,f?" is":" is not");
}
}
05.3方法练习2
自增
int n = 5;
int result = ++n; // n 变成 6,result 也等于 6
int n = 5;
int result = n++; // result 是 5,n 变成 6
// 后缀自增 n++
// 1. 先使用当前值 5 赋给 result
// 2. 然后 n 增加 1
方法传参
static int AddOne(int n)
{
int a = n + 1;
return a;
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int result = AddOne(n);
Console.WriteLine(result);
}
方法重载和可变长参数列表
06 类和对象
Modifiers: ctrl alt shift