using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _41_方法的综合练习
{
class Program
{
static void Main(string[] args)
{
//提示用户输入两个数字,计算这两个数字之间(包括这两个数)所有整数的和
//1.用户只能输入数字
//2.计算两个数字之和
//3.要求第一个数字必须比第二个数字小,否则重新输入
Console.WriteLine("请输入第一个数字:");
string strNumberOne = Console.ReadLine();
int numberOne = GetNumber(strNumberOne);
Console.WriteLine("请输入第二个数字:");
string strNumberTwo = Console.ReadLine();
int numberTwo = GetNumber(strNumberTwo);
//判断第一个数字是否小于第二个数字
JudgeNumber(ref numberOne, ref numberTwo);
//求和
int sum = GetSum(numberOne, numberTwo);
Console.WriteLine(sum);
Console.ReadKey();
}
/// <summary>
/// 判断输入是否为int类型
/// </summary>
/// <param name="s">输入的内容</param>
/// <returns>int类型的值</returns>
public static int GetNumber(string s)
{
while (true)
{
try
{
int number = Convert.ToInt32(s);
return number;
}
catch
{
Console.WriteLine("输入错误,请重新输入:");
s = Console.ReadLine();
}
}
}
/// <summary>
/// 判断第一个数是否比第二个数小
/// </summary>
/// <param name="n1">用户输入的第一个数</param>
/// <param name="n2">用户输入的第二个数</param>
public static void JudgeNumber(ref int n1, ref int n2)
{
while (true)
{
if (n1 < n2)
{
//符合题意
return;
}
else
{
Console.WriteLine("第一个数字不能大于或等于第二个数字,请重新输入第一个数字:");
string s1 = Console.ReadLine();
n1 = GetNumber(s1);
Console.WriteLine("请重新输入第二个数字:");
string s2 = Console.ReadLine();
n2 = GetNumber(s2);
}
}
}
/// <summary>
/// 计算两个数字之间所有整数的和
/// </summary>
/// <param name="n1">头</param>
/// <param name="n2">尾</param>
/// <returns>返回的总和值</returns>
public static int GetSum(int n1, int n2)
{
int sum = 0;
for (int i = n1; i <= n2; i++)
{
sum += i;
}
return sum;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _42_方法的最后练习
{
class Program
{
static void Main(string[] args)
{
#region 1.有一个字符串数组,用一个方法来实现,输出最长的字符串
////1.有一个字符串数组,用一个方法来实现,输出最长的字符串
//string[] names = { "花泽香菜", "水濑祈", "纮糖", "一之赖美琴", "白" };
////string s = "123456";
////Console.WriteLine(s.Length);
////Console.WriteLine(names.Length);
//string longest = Getlongest(names);
//Console.WriteLine(longest);
#endregion
#region 2.用方法来实现,请计算出一个整型数组的平均值,保留两位小数
////2.用方法来实现,请计算出一个整型数组的平均值,保留两位小数
//int[] a = new int[10];
//for (int i = 0; i < 10; i++)
//{
// a[i] = Convert.ToInt32(Console.ReadLine());
//}
//double avg = GetAvg(a);
////Console.WriteLine("{0:0.00}", avg);//仅在输出是改变,并未改变其本质
////保留两位小数 (下面两种方法都是四舍五入)
//string s = avg.ToString("0.00");
//avg = Convert.ToDouble(s);
//Console.WriteLine(avg);
////Console.WriteLine(avg.ToString("0.00"));
#endregion
#region 3.写一个方法,用来判断用户输入的数字是否为质数
//3.写一个方法,用来判断用户输入的数字是否为质数
//再写一个方法,要求用户只能输入数字,输入有误就让用户一直输入
//直到输入质数,程序才会结束
while (true)
{
Console.WriteLine("请输入一个数字,将会判断它是否为质数");
string strNumber = Console.ReadLine();
int number = GetNumber(strNumber);
bool b = IsPrime(number);
Console.WriteLine(b);
if (b == true)
{ break; };
}
#endregion
Console.ReadKey();
}
/// <summary>
/// 求最长的字符串
/// </summary>
/// <param name="s">输入的字符串数组</param>
/// <returns>返回最长的字符串</returns>
public static string Getlongest(string[] s)
{
string max = s[0];
for (int i = 0; i < s.Length; i++)
{
if (s[i].Length > max.Length)
{
max = s[i];
}
}
return max;
}
/// <summary>
/// 计算一个整型数组的平均值,保留两位小数
/// </summary>
/// <param name="nums">整型数组</param>
/// <returns>返回数组的平均值</returns>
public static double GetAvg(int[] nums)
{
double sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
}
return sum / nums.Length;
}
/// <summary>
/// 判断用户输入的是否为整数
/// </summary>
/// <param name="strNumber">用户输入的数据</param>
/// <returns>返回整数</returns>
public static int GetNumber(string strNumber)
{
while (true)
{
try
{
int number = Convert.ToInt32(strNumber);
return number;
}
catch
{
Console.WriteLine("输入数据不是整数,请重新输入:");
strNumber = Console.ReadLine();
}
}
}
/// <summary>
/// 判断一个属是否为质数
/// </summary>
/// <param name="number">用户输入的数</param>
/// <returns>返回是否为整数bool类型</returns>
public static bool IsPrime(int number)
{
if (number < 2)
{
return false;
}
else
{
//让这个数字从2开始除 除到自身的前一位
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
//给非质数准备的
return false;
}
}
//给质数准备的
return true;
}
//return true; //放在这里也行
}
}
}
using System;
namespace _043_方法的最后练习2
{
class Program
{
static void Main(string[] args)
{
#region 1.接受输入后判断其等级并显示出来
////1.接受输入后判断其等级并显示出来
//Console.WriteLine("请输入考试成绩:");
//int score = Convert.ToInt32(Console.ReadLine());
//string level = getLevel(score);
//Console.WriteLine(level);
#endregion
#region 2.将字符串数组{"中国","美国","巴西","澳大利亚","加拿大"}中的内容反转
////2.将字符串数组{"中国","美国","巴西","澳大利亚","加拿大"}中的内容反转
//string[] names = { "中国", "美国", "巴西", "澳大利亚", "加拿大" };
//Reverse(names);
//for (int i = 0; i < names.Length; i++)
//{
// Console.WriteLine(names[i]);
//}
#endregion
#region 3.计算圆的周长和面积
////3.计算圆的周长和面积
//Console.WriteLine("请输入圆的半径:");
//double r = Convert.ToDouble(Console.ReadLine());
//double perimeter;
//double area;
//GetPerimeterArea(r,out perimeter,out area);
//Console.WriteLine("圆的周长:{0:0.00},面积{1:0.00}", perimeter, area);
#endregion
Console.ReadKey();
}
/// <summary>
/// 判断学生等级
/// </summary>
/// <param name="score">输入学生的分数</param>
/// <returns>返回学生的等级</returns>
public static string getLevel(int score)
{
string level = "";
switch (score / 10)
{
case 10: level = "A+"; break;
case 9: level = "A"; break;
case 8: level = "A-"; break;
case 7: level = "B+"; break;
case 6: level = "B"; break;
default:
level = "C";
break;
}
return level;
}
/// <summary>
/// 将数组元素反转
/// </summary>
/// <param name="names">要被反转的数组</param>
public static void Reverse(string[] names)
{
for (int i = 0; i < names.Length/2; i++)
{
string temp = names[i];
names[i] = names[names.Length - 1 - i];
names[names.Length - 1 - i] = temp;
}
}
/// <summary>
/// 计算圆的周长和面积
/// </summary>
/// <param name="r">圆的半径</param>
/// <param name="perimeter">额外返回圆的周长</param>
/// <param name="area">额外返回圆的面积</param>
public static void GetPerimeterArea(double r,out double perimeter,out double area)
{
perimeter = 2 * 3.14 * r;
area = 3.14 * r * r;
}
}
}
using System;
namespace _044_方法的最后练习3
{
class Program
{
static void Main(string[] args)
{
#region 1.计算任意多个数的和(params)
////1.计算任意多个数的和
//int sum = GetSum(1, 2, 3, 4, 5, 6, 7, 8);
//Console.WriteLine(sum);
#endregion
#region 2.通过冒泡排序算法对数组实现升序排序
////2.通过冒泡排序算法对数组{3,2,56,43,8,76,23,1,87}实现升序排序
//int[] nums = { 3, 2, 56, 43, 8, 76, 23, 1, 87 };
//Sort(nums);
//for (int i = 0; i < nums.Length; i++)
//{
// Console.WriteLine(nums[i]);
//}
#endregion
#region 3.将一个字符串数组输出为|分割的形式
////3.将一个字符串数组输出为|分割的形式
//string[] names = { "蕾姆", "是", "我的", "老婆" };
//string str = ProcessString(names);
//Console.WriteLine(str);
#endregion
Console.ReadKey();
}
/// <summary>
/// 计算多个数的和
/// </summary>
/// <param name="nums">输入的数</param>
/// <returns>返回的总和</returns>
public static int GetSum(params int[] nums)
{
int sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
}
return sum;
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="nums">需要排序的数组</param>
public static void Sort(int[] nums)
{
for (int i = 0; i < nums.Length-1; i++)
{
for (int j = 0; j < nums.Length-1-i; j++)
{
if (nums[j] > nums[j + 1])
{
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
/// <summary>
/// 将一个字符串数组输出为|分割的形式
/// </summary>
/// <param name="names">输入的数组</param>
/// <returns>返回为|分割的形式</returns>
public static string ProcessString(string[] names)
{
string str = null;
for (int i = 0; i < names.Length-1; i++)
{
str += names[i] + '|';
}
return str + names[names.Length - 1];
}
}
}