Console.Read()读取一个字符,返回该字符的ascII码,Console.ReadLine()是读取一行,返回值是string类型。
console.readline相关话题
请问在这里Console.Readline()起到什么作用?为什么我删掉就会报错?namespace ConsoleApp4{ class Program { static void Main(string[] args) { int x = 1; int y = 2; int z = x + y; Console.WriteLine(z); Console.WriteLine("输入你的名字"); Console.Write("输入你的姓:"); string myfirstname; myfirstname = Console.ReadLine();//这行删掉会报错 string mylastname; Console.Write("输入你的名:"); mylastname = Console.ReadLine(); Console.WriteLine("hello" + myfirstname + mylastname); Console.ReadLine();
展开
你指着 Console.ReadLine(),就会说明这个方法的含义;它表示从控制台接受一行数据你把string myfirstname;改为string myfirstname="";就不会报错了因为你使用了myfirstname这个变量,但是你却没给它赋值,所以它就报错:未将对象的引用添加到对象的实例,解决办法就是在使用变量前做空值检查或者给它一个初始值
追问
再请教下为什么我加一行string abc;就只会出现警告而不会出现错误。可是string myfirstname;就会出现错误呢。
追答
错误并不可怕,可怕的是并不知道为什么出错显然你这个问题很简单,你鼠标指着错误的地方,它会提示你是什么错误,这是visual studio的官方错误提示,如果不懂可以百度搜索,你也可以在错误列表里面看到所有的错误string myfirstname的意思是声明变量myfirstname为string类型,一个变量不能被声明两次的就是这样;把string去掉就可以了(一个变量可以被多次赋值)
追问
其实我是想问下同样是定义string格式,为什麼string abc不加Console.ReadLine()就不会报错,而string myfirstname不加就会报错,按理说格式是一样的啊。难道说myfirstname这个名称在系统裏有特殊定义吗?谢谢
追答
那你看了出错的地方在哪里吗?你前面怎么赋值都没事,是不会出问题的出问题的地方在倒数第二行代码是一个前后关联的东西,你出错是因为没有对myfirstname赋值,又去读取它的值所以你需要在前面给它赋值
更多追问
c,email = Console.ReadLine().Trim()中为什么要用Trim除去首尾空格?
不为什么,如果你能保证不会多输入空格的话,当然也可以不写trim
1、用面向对象程序设计方法设计一个简单的计算器程序,该程序能完成简单的整数的加减乘除运算:其输入运行如下: C:> java calc 20 + 8 C:> 20 + 8 = 28
using System;
using System.Collections.Generic;
using System.Text;
{
/// <summary>
/// 此示例演示计算机两个数的加减乘除取余
/// </summary>
class Program
{
static void Main(string[] args)
{
int first; //第一个操作数
int second; //第二个操作数
string myOprator; //运算符
int result; //计算结果
//输入参与计算操作数和运算符
Console.WriteLine("我是第一个简单的计算器,可以实现两个整数的加减乘除取余")
Console.Write("请输入第一个操作数:");
first = int.Parse(Console.ReadLine());
Console.Write("请输入第二个操作数");
second = int.Parse(Console.ReadLine());
Console.Write("请输入运算符(+ - * %):");
myOprator = Console.ReadLine();
//计算,并输出运算符
if (myOprator == "+") //加法
{
result = first + second;
Console.WriteLine("加上等于", first, second, result);
}
else if (myOprator == "-") //减法
{
result = first - second;
Console.WriteLine("减去等于", first, second, result);
}
else if (myOprator == "*") //乘法
{
result = first * second;
Console.WriteLine("乘以等于", first, second, result);
}
else if (myOprator == "%") //取余
{
result = first % second;
Console.ReadLine();
}
要求用动态数组的储该班的学生信息,统计不及格(小于60分)、优秀(大于等于90分)的学生人数。可以查询、增加、删除、编辑学生。
using System; using System.Collections.Generic; using System.Text; namespace stuConsole { //定义学生类 class student { //字段定义 private string name; //姓名 private string no; //学号 private double engScore; //英语成绩 private double cScore; //C++成绩 private double mathScore; //数学成绩 //定义属性,通过属性访问、修改相应字段 public string Name { set{name = value;} get{return name;} } public string No { set{no = value;} get{return no;} } public double EngScore { set{engScore = value;} get{return engScore;} } public double CScore { set{cScore = value;} get{return cScore;} } public double MathScore { set { mathScore = value; } get { return mathScore; } } //输入相应的数据 public void inputText() { Console.WriteLine("请输入姓名:"); Name = Console.ReadLine(); Console.WriteLine("请输入学号:"); No = Console.ReadLine(); Console.WriteLine("请输入英语成绩:"); EngScore = double.Parse(Console.ReadLine()); Console.WriteLine("请输入C++成绩:"); CScore = double.Parse(Console.ReadLine()); Console.WriteLine("请输入数学成绩:");
之前提问过一次,但后来发觉答案不理想using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _3333{class a{delegate void tiga();public void hello(){Console.WriteLine("tiga is very strong");}static void Main(string[] args){tiga w = new tiga(hello);hello();Console.ReadLine();}}}
展开
你的hello函数有问题,既然你要引用它,又没有申明,就只能用静态了。改成下面这样:
class a { public static void hello() { Console.WriteLine("tiga is very strong"); } static void Main(string[] args) { hello(); Console.ReadLine(); } }
上面的代码来看,你定义的委托没有什么作用,你想把函数名当做参数来用,也不是这样用的啊。。
追问
那不用静态行不行?不用static 只用public
追答
class a { class newClass { public void hello() { Console.WriteLine("tiga is very strong"); } } static void Main(string[] args) { newClass newclass = new newClass(); newclass.hello(); Console.ReadLine(); } }
创建一个学生结构体类型,包含学号,姓名,性别,成绩,输入10个学生的信息,输出10个学生的信息。求10个学生的平均成绩,求10个学生的最高成绩和最低成绩
using System;namespace ConsoleApplication1{ /// <summary> /// 学生结构体 /// </summary> public struct Student { /// <summary> /// 学号 /// </summary> public int Id; /// <summary> /// 姓名 /// </summary> public string Name; /// <summary> /// 性别 /// </summary> public string Gendar; /// <summary> /// 成绩 /// </summary> public float Score; public override string ToString() { return string.Format("学号={0}, 姓名={1}, 性别={2},成绩={3}", Id, Name, Gendar, Score); } } class Program { static void Main(string[] args) { Student[] students = new Student[10]; //输入10个学生信息 for(int i=0; i<10; i++) { Console.Write("输入学号:"); string input = Console.ReadLine(); int id = int.Parse(input); Console.Write("输入姓名:"); string name = Console.ReadLine(); Console.Write("输入性别:"); string gender = Console.ReadLine(); Console.Write("输入成绩:"); input = Console.ReadLine(); float score = float.Parse(input); students[i] = new Student { Id = id, Name = name, Gendar = gender, Score = score }; } // 输出10个学生的信息 for(int i=0; i<10; i++) { Console.WriteLine(students[i].ToString()); } // 平均成绩 float total = 0; for(int i=0; i<10; i++) { total += students[i].Score; } Console.WriteLine("平均成绩:{0}", total / students.Length); //最高成绩和最低成绩 float max = 0, min = 100; for(int i=0; i<10; i++) { if (students[i].Score > max) max = students[i].Score; if (students[i].Score < min) min = students[i].Score; } Console.WriteLine("最高成绩:{0}", max); Console.WriteLine("最低成绩:{0}", min); } }}
大概是这个样子,求大神们能不能照着图帮我修改正确
public static void Sort(int[] array){int[] arr=new int[]{45,12,44,4,5};for(int j=1;j<arr.Length;j++){//外循环每次把参与排序的最大数排在最后for(int i=0;i<arr.Length-j;i++){ //内层循环负责对比相邻的两个数,并把最大的排在后面if(arr[i]>arr[i+1]){ //如果前 一个数大于后一个数,则交换两个数int temp=arr[i];arr[i]=arr[i+1];arr[i+1]=temp;}}}排序已成,你价格输出就行
追问
排序我会,就是不会插入并输出
_§:з)))」∠)_
c一个文本文件存放一个数组,每行存放一个数字求帮忙
&xe671;
&xe673;
(1)文本文件中的数据按行存放,每行一个数据,数据的数量不定,可多可少。从文本文件中读入的数据并转换后,先存放泛型集合List<T>,最后再将List<T>转换成一维数组。(2)实现代码:文本文件 D:&92;data.txt 中存放的数据为浮点类型,每行一个数据123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 using System;using System.Collections.Generic;using System.IO;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string filePath = @"d:&92;data.txt"; // 从数据文件读取数据 float[] values = GetDataFromFile(filePath); // 显示数据 for (int i = 0; i < values.Length; i++) { Console.WriteLine(values[i]); } Console.WriteLine("按Enter键退出"); Console.ReadLine(); } static float[] GetDataFromFile(string datafilePath) { // 创建泛型列表 List<float> list = new List<float>(); // 打开数据文件 D:&92;data.txt逐行读入 StreamReader rd = File.OpenText(datafilePath); string line; while ((line = rd.ReadLine()) != null) { // 将读入的数据转换成float类型值 float result; if (float.TryParse(line, out result)) { // 转换成功,加入到泛型列表 list.Add(result); } else { // 转换失败,显示错误提示 Console.WriteLine("数据格式错误!"); } } // 关闭文件 rd.Close(); // 将泛型列表转换成数组 return list.ToArray(); } }}
string condtr = "server=localhost;User Id=root;password=zyz;Database=cd"; MySqlConnection mycon = new MySqlConnection(condtr); mycon.Open(); MySqlCommand mycmd = new MySqlCommand("select * from cd1 where 英文=&39;" + textBox1.Text + "&39;", mycon); MySqlDataReader mydr = mycmd.ExecuteReader(); if (mydr.HasRows) { while (mydr.Read()) { textBox2.Text = mydr["英文"] + "&92;n"; DataGridViewRow dgvr = new DataGridViewRow(); dgvr.CreateCells(dataGridView1, new object[] { "查询", textBox1.Text, textBox2.Text }); dataGridView1.Rows.Add(dgvr); } Console.ReadLine(); mycon.Close(); } else { MessageBox.Show("词典暂无此单词"); }
展开
string i = "select sum(toolNumber) from mtctoolstable where tooltime between year1+month1 and year2+month2 and toolsign=0 and toolName=toolName"; 第二个toolName应该是个参数吧,应该 string i = "select sum(toolNumber) from mtctoolstable where tooltime between year1+month1 and year2+month2 and toolsign=0 and toolName=&39;"+toolName+"&39;"; 然后你的year1+month1 你看下结果是什么,是日期的格式吗?应该不是吧。 格式相同才能比较撒。 如果2008-1-1和20081 怎么比较。
追问
我现在有另一个问题 你有时间吗