赞
踩
编程实现在一行内输入若干个整数(不超过30个),输出最大的一个整数。
样例如下:
输入(在一行内输入,空格分隔):
5 6 78 -89 0 23 100 4 6
输出:
100
C#具体设计:
1.先让用户输入一些整数,
2.再装其读入到字符串用,
3.用空格符来分割字符成,存成字符数组
4.用foreach语句进行数组遍历,将字符转成整型后,比较大小,找出最大整数
5.输出最大整数
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { System.Console.Write("Please Input Number..."); string strnums = Console.ReadLine(); string[] arr = strnums.Split(new char[] { ' ' }); int max = int.MinValue; foreach(string str in arr) { int temp = Convert.ToInt32(str); if (max < temp) max = temp; } Console.WriteLine(max); Console.Read(); } } }
输入一个总的秒数,将该秒数换算为相应的时、分、秒。
如输入3600秒,则输出结果为1小时;输入3610秒,结果为1小时10秒。
样例1:
3601
1小时1秒
样例2:
67
0小时1分7秒
解:从题上分析可得出,先要用户输入秒数,然后进行转换成时、分、秒等,最后将计算结果按指定要求输出。
1.用户输入秒数,读入秒数,转换成int整型
2.换算成时、分、秒,具体方法就是3600秒等于1小时,60秒等于1分钟,从小时开始换算起
3.按要求输出换算的结果
4.最后将代码放入while循环中,用户将可以一直输入
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { while (true) { Int32 Seconds = Convert.ToInt32(Console.ReadLine()); int hour = Seconds / 3600; Seconds -= hour * 3600; int minu = Seconds / 60; Seconds -= minu * 60; int sec = Seconds; if(minu == 0) Console.WriteLine("{0}小时{1}秒", hour, sec); else Console.WriteLine("{0}小时{1}分{2}秒", hour, minu, sec); } } }
运行结果:

某城市最高的楼只有一部电梯。该电梯和一般电梯不同的是它依照输入楼层数的先后次序运行。电梯最初在0层。运行完一个输入序列后就停止在该楼层,不返回0层。编写程序计算电梯运行一个序列的时间。每次都假设电梯在0层开始,无论上一次运行到几层。电梯每上1层需要6秒。每下1层需要4秒。如在某层停留,无论上下人多少,均停留5秒。
输入:楼层的值大于等于1,小于100 ,N=0表示结束输入。
输出:计算每个序列电梯运行的时间。
输入:
2 1 0
输出:
26
题目分析:
1.先读入输入的字符串,也就是电梯的楼层数,
2.将读入字符串进行分割,分割字符用空格“ ”就可以了
3.用for循环进行遍历字符串数组,计算时间,当循环到0层时,就退出循环
4.输出计算的用时
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { int preLevel = 0; int Seconds = 0; string str = Console.ReadLine(); string[] strarr = str.Split(' '); for(int i=0;i< strarr.Length; i++) { int Level = Convert.ToInt32(strarr[i]); if (Level == 0) break; int LevNum = Level - preLevel; if (LevNum != 0) Seconds += 5; if (LevNum > 0) Seconds += 6 * Math.Abs(LevNum); else if(LevNum < 0) Seconds += 4 * Math.Abs(LevNum); preLevel = Convert.ToInt32(strarr[i]); } Console.WriteLine(Seconds); Console.ReadKey(); } } }
利用一维数组求解问题。读入若干(1-15个)整数(一行输入,空格分隔),每个数在10-100之间的整数包括10和100。在读入每个数时,确认这个数的有效性(在10到100之间),并且若它和之前读入的数不一样,就把它存储到数组中,无效的数不存储。读完所有数之后,仅显示用户输入的不同的数值。
样例1:
输入:
12 34 99 123 12 123 78 0 12 99
输出:
12 34 99 78
样例2:
输入:
-9 -9 0 34 99 99 99 34 34 34
输出
34 99
题目分析:
1.读入输入字符串,用空格进行分割字符串,变成字符串数组
2.用for循环遍历字符串数组,将其转化成int整型,判断数是否有效,判断数组中是否存在此数,不存在就加入到数组中
3.用foreach语句按题目要求格式输出数组中的数字
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { string str = Console.ReadLine(); string[] strarr = str.Split(' '); List<int> iList = new List<int>(); for (int i = 0; i < strarr.Length; i++) { int num = Convert.ToInt32(strarr[i]); if (num > 100 || num < 10) continue; bool CheckNum = false; for (int j = 0; j < iList.ToArray().Length; j++) { if (num == iList[j]) { CheckNum = true; break; } } if (!CheckNum) iList.Add(num); } foreach(int i in iList) { Console.Write("{0} ", i); } Console.Read(); } } }
开发计算机辅助教学程序,教小学生学乘法。程序功能:
(1)程序开始时让用户选择“年级”为1或2。一年级使只用1位数乘法;二年级使用2位数乘法。
(2)用Random对象产生两个1位或2位正整数,然后输入以下问题,例如:How much is 6 times 7?然后学生输入答案,程序检查学生的答案。如果正确,则打印“Very good!”,然后提出另一个乘法问题。如果不正确,则打印“No,Please try again.”,然后让学生重复回答这个问题,直到答对。
(3)答对3道题后程序结束。
(4)使用一个单独方法产生每个新问题, 这个方法在程序开始时和每次用户答对时调用。
分析:
1.让用户输入1或2年级
2.进行3次循环(练习3次全对),循环体内有出题函数,判断输入答案是否正确
3.出题函数设计,三个参数,第一个是输入年级,2、3两个参数是根据年级来随机生成的乘数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { public static void GenerateTwoNum(int Level, out int miu1, out int miu2) { Random Rand = new Random(); int min = 0; int max = 0; if (Level == 1) { min = 1; max = 10; } else if (Level == 2) { min = 10; max = 100; } miu1 = Rand.Next(min, max); miu2 = Rand.Next(min, max); } static void Main(string[] args) { Console.WriteLine("请输入年级 1:一年级 2:二年级"); string str = Console.ReadLine(); int level = Convert.ToInt32(str); int Count = 0; while (Count < 3) { int miu1; int miu2; GenerateTwoNum(level, out miu1, out miu2); Console.WriteLine("How much is {0} times {1} ?", miu1, miu2); while (Convert.ToInt32(Console.ReadLine()) != miu1 * miu2) { Console.WriteLine("No,Please try again"); } Console.WriteLine("Very good!"); Count++; } } } }
结果:

编写一个程序,含有1个ComboBox控件和1个ListBox控件。在ComboBox中显示9种状态名。从ComboBox中选择一个项目时,将其从ComboBox删除并加ListBox中。程序要保证ComboBox中至少有一个项目,否则用消息框打印一个消息,然后在用户关闭消息框时终止执行程序。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); AddComBox(); } private void AddComBox() { comboBox.Items.Add("Q"); comboBox.Items.Add("W"); comboBox.Items.Add("E"); comboBox.Items.Add("R"); comboBox.Items.Add("T"); comboBox.Items.Add("Y"); comboBox.Items.Add("U"); comboBox.Items.Add("I"); comboBox.Items.Add("O"); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { listBox.Items.Add(comboBox.SelectedItem.ToString()); comboBox.Items.RemoveAt(comboBox.SelectedIndex); if (comboBox.Items.Count == 0) { if (DialogResult.OK == MessageBox.Show("没有数据项")) Close(); } } } }
运行结果:

定义圆类Circle,包含半径r,属性R能判断半径r的合理性(r>=0),计算圆面积的方法double Area()。
从Circle类派生出圆柱体类Cylinder类,新增圆柱体的高h,属性H能判断高h的合理性(h>=0),新增计算圆柱体体积的方法double Volume()。
在主方法中,创建一个Cylinder对象,并输出该对象的底面半径,高以及体积。(要求:不使用构造方法,并且类中的域为私有,方法为公有)。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { Cylinder cylinder = new Cylinder(); cylinder.R = 10; cylinder.H = 20; cylinder.show(); Console.Read(); } } class Circle { private double r; public Circle() { R = 0; } public Circle(double r) { R = r; } public double R { set { r = R >= 0 ? value : 0; } get { return r; } } public double Area() { return Math.PI * r * r; } public virtual void show() { Console.WriteLine("圆的面积:{0}", Area()); } } class Cylinder : Circle { private double h; public double H { set { h = H >= 0 ? value : 0; } get { return h; } } public double Volume() { return Area() * h; } public override void show() { base.show(); Console.WriteLine("圆柱的体积:{0}", Volume()); } } }
测试结果:

使用图形方法,在Form上画出:
画出5不同颜色直线并形成一个多边形;
用红色画笔画一个圆形;
3.用图片填充一个矩形。、
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Random rand = new Random(); Point[] p = new Point[5]; private void Form1_Paint(object sender, PaintEventArgs e) { Graphics grap = e.Graphics; p[0] = new Point(100, 100); p[1] = new Point(200, 200); p[2] = new Point(100, 300); p[3] = new Point(000, 200); p[4] = new Point(100, 100); Pen pen = new Pen(Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)),2); grap.DrawLines(pen, p); Pen redpen = new Pen(Color.Red, 2); grap.DrawArc(redpen, new Rectangle(new Point(300, 50), new Size(80, 80)), 0, 360); grap.FillRectangle(new TextureBrush(new Bitmap("1.bmp")), new Rectangle(new Point(300, 200), new Size(80, 80))); } } }
测试结果:

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。