添加答案
添加答案
char
类型是一个16位的Unicode字符,而int
类型是一个32位的有符号整数。因为char
类型的取值范围是0到65535,这是一个完全容纳于int
类型范围内的值,所以从char
到int
的转换是隐式的,编译器可以自动处理这种类型转换。
然而,short
类型是一个16位的有符号整数,它的取值范围是-32768到32767,而char
类型的取值范围是0到65535。尽管两者的位数相同,但它们的取值范围不完全重叠,因此char
到short
的转换被视为不安全的转换,需要显式地进行类型转换。
//编写一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下:
//'a'→'d' b'→'e’ 'w'→'z' …. x'→'a’'y'→'b''z'→'c' 'A'→'D'"B'→'℉'"W'→ 'Z' .. "X→'A' 'Y'→'B’ Z'→'C'
//对于其他字符,不进行加密。
string zifuchuan = Console.ReadLine();//用户输入字符串
char[] shuzu = zifuchuan.ToCharArray ();//将字符串转换成数组
for(int i = 0; i < shuzu.Length; i++) //遍历数组 .Length 是获取数组中元素的总数
{
if (('a' <= shuzu[i] && shuzu[i] <= 'z')||('A' <= shuzu[i] && shuzu[i] <= 'Z')) //判断是否为小写字母 或者是大写字母
{
shuzu[i] = (char)(shuzu[i] + 3); //强制类型转换 将数组中属于a-z或者A-Z的字母往后移3位
if(shuzu[i]>'z' && shuzu[i] < 'z' + 4) //判断是否是字母x,y,z
{
shuzu[i] = (char)(shuzu[i] - 26); //将xyz往前移26位
}
if (shuzu[i] > 'Z' && shuzu[i] < 'Z' + 4) //判断是否是字母X,Y,Z
{
shuzu[i] = (char)(shuzu[i] - 26); //将XYZ往前移26位
}
}
}
foreach (char temp in shuzu) //使用foreach循环 遍历数组
{
Console.Write(temp);
}
Console.ReadKey();
int he = 364; //喝的可乐
int ping = 364; //空瓶子
while (ping >= 3) //当空瓶子大于或等于3时
{
he += ping / 3; //喝的可乐=原来的364瓶 + 空瓶子/3
ping = ping / 3 + ping % 3; //空瓶子 = 空瓶子/3 + 余下的空瓶子
}
Console.WriteLine("可以喝{0}瓶可乐"+" "+"剩下{1}个空瓶",he,ping);
Console.ReadKey();
// 3个可乐瓶可以换一瓶可乐,现在有364瓶可乐,问一共可以喝多少瓶可乐,剩下几个空瓶。
int he = 0; //喝的可乐
int ping = 0; //剩下的瓶子
for (int a = 364; a > 0; a--) //当可乐大于0时 递减
{
he++; //喝了的可乐递加
ping++; //空瓶子递加
if(ping >= 3) //当空瓶子大于或等于3时
{
a++; //总计可乐+1
ping=ping % 3;//瓶子除以3
}
}
Console.WriteLine("可以喝{0}瓶可乐"+" "+"剩下{1}个空瓶",he,ping);
Console.ReadKey();
练习1:
int num1 = convert.toint32(console.readline());
int num2 = convert.toint32(console.readline());
console.writeline(num1+num2);
练习2:
int num1 = convert.toint32(console.readline());
int num2 = convert.toint32(console.readline());
console.writeline((int)((num1+num2)/2));
1、ReadLine(),返回一个字符串(输入)
2、类型转换(系统自带):Convert.ToInt32()
d
解密
int a = Convert.ToInt32(Console.ReadLine());
int b = (a / 1000)%10;
int c=(a % 100)/10;
Console.WriteLine(""+b+c);
1
acd
2
b
3
acd
4
int number = Convert.ToInt32(Console.ReadLine());
int ge = number % 10;
int shi = (number / 10) % 10;
int bai = number /100;
Console.WriteLine(""+ge+shi+bai);
5
int number = Convert.ToInt32(Console.ReadLine());
int shi = (number / 10) % 10;
int qian = (number /1000)%10;
int a = qian * 10 + shi;
char d = (char)a;
Console.WriteLine(d);
6
7;8
7
int math =Convert.ToInt32(Console.ReadLine());
int english =Convert.ToInt32(Console.ReadLine());
bool a = math > 90 && english > 90;
Console.WriteLine(a);
1
int a = Convert.ToInt32(Console.ReadLine());
int b= Convert.ToInt32(Console.ReadLine());
int c = a + b;
Console.WriteLine(c);
2.
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = a + b;
double ave = c / 2;
Console.WriteLine(ave);
3.
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
double c= a/ b;
Console.WriteLine(c);
4.
1;2;3;5;
5.
a
6.
a
convert.toint32(str);//只能转化数字字符串
1
3 ;6
2
Console.WriteLine("1\n\t2\n3");
3
Console.Write("*\n");
Console.Write("**\n");
Console.Write("***\n");
Console.Write("****\n");
Console.Write("*\n");
Console.Write("*\n");
Console.WriteLine("*");
4
Console.Write(" *\n");
Console.Write(" ***\n");
Console.Write(" *****\n");
Console.Write("*******\n");
Console.Write(" *\n");
Console.Write(" *\n");
Console.Write(" *\n");
5
d
6
Console.WriteLine("SiKi说:\"what is \\n\"");
看了两遍,第二遍边看边做,变想,一行代码一行代码的尝试理解,这才基本理解了。把自己整理了三个小时的笔记分享出来希望可以帮到像我一样的新手,不尽不实之处还望老师和学长指正!
注释ctrl+E+C
ctrl+k + ctrl+c
解除注释ctrl+k + ctrl+u
ctrl+E+C ->注释
writeline ->输出
//注释 \n换行 \\输出\ \"输出"
framework是需要添加readkey来保持窗口不被瞬间关闭
第一个游戏作品问世啦哈哈哈
Console.Title = "我猜,我猜,我猜猜猜";
Console.WriteLine("欢迎进入猜数字小游戏,系统已随机生成一个1-100的自然数,请输入数字进行竞猜");
Random random = new Random();
int a = random.Next(1, 1000);
for (int b = Convert.ToInt32(Console.ReadLine()); b != a; b = Convert.ToInt32(Console.ReadLine()))
{ if (b < a) { Console.WriteLine("恭喜你猜小了,结果不是{0}请继续竞猜", b); } else { Console.WriteLine("恭喜你猜大了,结果不是{0}请继续竞猜", b); } }
Console.WriteLine("行,你行,你真行啊,这你都猜到了,答案就是{0}", a);
Console.ReadKey();
独立解题连解题思路都和老师一样哈哈,显摆一下:
int q = Convert.ToInt32(Console.ReadLine());//2
int n = Convert.ToInt32(Console.ReadLine());//3
int sum = 1;
int tol = 1;
//q的N次方
for (int i = 1; i <= n; i++) {//1+2+2*2+2*2*2+2*2*2*2= 3+4+8+16 =15+16=31
sum *= q;
tol += sum;
}
Console.WriteLine(tol);
3n演化:
int n = Convert.ToInt32(Console.ReadLine());
int cunt = 0;
while (n > 1) {
if (n % 2 != 0) { n = (3 * n + 1); cunt++; Console.WriteLine(n); }
else { n = n / 2; cunt++;Console.WriteLine(n); }
}
if (n == 1) {Console.WriteLine("经过{0}次演化,输入的数值演化为1",cunt); }
自己尝试了一下n1到n2之间的所有偶数:
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
while (n1 < n2+1) {
if (n1 % 2 == 0) { Console.WriteLine(n1); }
n1++;
}