The number of daffodils (must be three digits): the cube of the hundreds digit + the cube of the ten digit + the cube of each digit = the current number itself
for(int i=100; i <=999; i++)
{
int bai = i / 100;
int shi=i%100/10;
int ge=i%10;
if (bai*bai*bai+shi*shi*shi+ge*ge*ge==i)
{
Console.WriteLine(i);
}
}
output multiplication formula
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("{0}*{1}={2}\t", j, i, i * j);
}
Console.WriteLine();}
}
Console.ReadKey();
Type conversion: Convert/Parse/TryParse
(int) variable name [cast]:
This conversion method is mainly used for digital type conversion. From int type to long, float, double, decimal type, implicit conversion can be used. When converting from long type to int type, display conversion needs to be used, that is, the data Type conversion method, otherwise a compilation error will occur
This method will unconditionally round off floating-point numbers and lose precision.
The method can also remember the conversion of object to int, but the value of object must be assigned the value of type int, otherwise a compilation error will occur, and an error will be returned when object is null
Also, it cannot be used to convert char type to int type, otherwise the returned value is ASCII code, which is not the result you want
int.Parse(string thunder star variable):
This method is to convert the string of digital content to int type. If the string content is empty or null, an ArgumentNullException will be thrown. If the spontaneous content is not a number, a FormatException will be thrown. The number represented exceeds the range that can be represented by the int type, and an OverFlowException is thrown.
When using this method can only process the content of the string, and the content of the string can only be within the range that the int type can represent
int.TryParse(string s,out int result):
This method is also to convert the string of digital content to int type, but this method is superior to int.Parse in that it does not throw an exception, if the conversion is successful, it returns true, if the conversion fails, it returns false, and the last return parameter is the output value, if the conversion is successful, the corresponding value is output, and if the conversion fails, it returns 0
Convert.ToInt32(string s):
This method can not only convert strings to int types, but also convert other types of values to int types. If the variable is of type object or string, when its value is null, it will return 0, which will not cause program errors. However, if the value of this string type is string.Empty, it will cause a program error when converting to int
This method will round up floating point numbers
The modified method also cannot care for the char type, otherwise the returned ASCII code is also
Find prime numbers from 1 to 100
Prime number: a number that can only be divided by 1 and itself, 2 is the smallest prime number, and a negative number is not a prime number
bool show = true;
for (int i = 2; i <= 100; i++)
{
for (int j = 2; j < i; j++)
{
if (i % j == 0)//not prime
{
show = false;
break;//If it is not a prime number, it will jump out of the current loop and not continue the inner loop
}
}
if (show)
{
Console.WriteLine(i);
}
show = true;
}
Console.ReadKey();
Ternary operator;
variable=expression1?expression2:expression3
First judge whether expression 1 is correct, if it is correct, the variable value is the result of expression 2, otherwise it is the result of expression 3
ps: The operation result of expression 2 and the operation result of expression 3 must be consistent with the types of the three variables
int b=6>4?6:4;//If 6 is greater than 4, assign 6 to b, otherwise assign 4 to b
To generate random numbers, you need to use Random
while (true)
{
Random rd = new Random();
int s = rd.Next(100, 999);
Console.WriteLine(s);
Console.ReadKey();
}
Time Seed:
code
for(int i=0;i<100;i++)
{
Random rd = new Random();
int s = rd.Next(100, 999);
Console.WriteLine(s);
}
Console.ReadKey();
with code
Random rd = new Random();
for(int i=0;i<100;i++)
{
int s = rd.Next(100, 999);
Console.WriteLine(s);
}
Conosole.ReadKey();
The results generated after running are very different. The first one creates an object inside the loop, which will cause a problem with the random number generated, while the latter one creates an object outside the loop, and the random number can be generated normally.
constant:
Variables modified by the keyword const, once declared, cannot be reassigned
enumerate:
The enumeration type and the int type are compatible with each other and can be directly converted to each other using strong type conversion. The default enumeration type starts from 0
public enum Sex
{
male=1,
female=2,
}
//enum cast to intint sw = (int)Sex.male;
Console.WriteLine(sw);
//int cast to enum
int b = 1;
Sex sec = (Sex)b;
Console.WriteLine( sec);
//Convert enumeration to string
Sex se = Sex.male;
Console.WriteLine(se.ToString());
//Convert string to enumeration
string na = "2";
Sex se2 = ( Sex)Enum.Parse(typeof(Sex), na);
Console.WriteLine(se2);
Console.ReadKey();
Struct type: in order to create multiple variables of different types at once
ps: here is no longer called a variable, but a field
public struct Person{
public string _name;//This is called a field, not a variable, add an underscore before naming a field
public int _age;
public char _gender;
}
// use as follows:
Person per1;
per1._gender = '男';
per1._name = "张三";
per1._age = 18;
Int32 is a structure that can be viewed