weixin_62955149 2022-04-28 12:10 采纳率: 84.6%
浏览 24
已结题

请问此题如何将我输入的结果打印在屏幕上(语言-c#)

问题遇到的现象和发生背景
将对象初始化之后可以输入但输出结果不正确
问题相关代码
using System;

namespace _5
{
class Program
{
static void Main(string[] args)
{
Citizen s = new Citizen();
s.Input();
s.Print();
}
}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace _5
{
class Citizen
{
public string name;
public string Name
{
get
{
return name;
}
}
public string gender;
public string Gender
{
get
{
return gender;
}
}
public string age;
public string Age
{
get
{
return age;
}
}
public string birthplace;
public string BirthPlace
{
get
{
return birthplace;
}
}
public string id;
public string Id
{
get
{
return id;
}
}
public string familyaddress;
public string FamilyAddress
{
get
{
return familyaddress;
}
}
public Citizen()
{
name = "David";
gender = "Male";
age = "20";
birthplace = "Nanyang,Henan";
id = "1000";
familyaddress = "0000";
}
public void Input()
{
Console.WriteLine("Please input your name:");
string a = Console.ReadLine();
Console.WriteLine("Please input your gender:");
string b = Console.ReadLine();
Console.WriteLine("Please input your age:");
string c = Console.ReadLine();
Console.WriteLine("Please input your birthplace:");
string d = Console.ReadLine();
Console.WriteLine("Please input your id:");
string e = Console.ReadLine();
Console.WriteLine("Please input your familyaddress:");
string f = Console.ReadLine();
}
public void Print()
{
Console.WriteLine("Your name is:{0}","a");
Console.WriteLine();
Console.WriteLine("Your gender is:{0}","b");
Console.WriteLine();
Console.WriteLine("Your age is:{0}","c");
Console.WriteLine();
Console.WriteLine("Your birthplace is:{0}","d");
Console.WriteLine();
Console.WriteLine("Your id is:{0}","e");
Console.WriteLine();
Console.WriteLine("Your familyaddress is:{0}","f");
Console.WriteLine();
}
}
}
运行结果及报错内容
Please input your name:
1
Please input your gender:
2
Please input your age:
3
Please input your birthplace:
4
Please input your id:
5
Please input your familyaddress:
6
Your name is:a

Your gender is:b

Your age is:c

Your birthplace is:d

Your id is:e

Your familyaddress is:f
我想要达到的结果
在主程序中调用s.Print 输出s. Input 中输入的值

  • 写回答

2条回答 默认 最新

  • CoderZ1010 Unity领域优质创作者 2022-04-28 13:27
    关注
    using System;
    
    public class Citizen
    {
        public string ID;
        
        public string Name;
    
        public string Gender;
    
        public string Age;
    
        public string Birthplace;
    
        public string Familyaddress;
    
        public Citizen() { }
    
        public void Input()
        {
            Console.WriteLine("Please input your id:");
            ID = Console.ReadLine();
            Console.WriteLine("Please input your name:");
            Name = Console.ReadLine();
            Console.WriteLine("Please input your gender:");
            Gender = Console.ReadLine();
            Console.WriteLine("Please input your age:");
            Age = Console.ReadLine();
            Console.WriteLine("Please input your birthplace:");
            Birthplace = Console.ReadLine();
            Console.WriteLine("Please input your familyaddress:");
            Familyaddress = Console.ReadLine();
        }
    
        public void Print()
        {
            string info = string.Format("ID:{0}\r\nName:{1}\r\nGender:{2}\r\nAge:{3}\r\nBirthplace:{4}\r\nFamilyaddress:{5}",
                ID, Name, Gender, Age, Birthplace, Familyaddress);
            Console.WriteLine(info);
        }
    }
    
    public class Program
    {
        static void Main(string[] args)
        {
            Citizen citizen = new Citizen();
            citizen.Input();
            citizen.Print();
            Console.ReadLine();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月13日
  • 已采纳回答 5月5日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"