小编Zor*_*oro的帖子

覆盖.ToString方法c#

好的,所以我在编写C#编程书时编写了这个程序(我试图在这里学习)并且它要求" 覆盖ToString()方法以返回所有数据成员 ".

我做得对吗?或者我只是成功地编写了编译但什么都不做的代码.ToString的目的是什么?

我花了大约30分钟来查看其他帖子,并没有想出来,所以我决定这样做.

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

namespace ConsoleApplication297
{
class Program
{
    static void Main(string[] args)
    {
        String name = "Stormtrooper";
        Employee s = new Employee(name);
        Console.WriteLine("The type of hire is a {0}", s.Name);
        Console.WriteLine("The identification number is {0}", s.Number);
        Console.WriteLine("The date of hire is {0} ABY", s.Date);
        Console.WriteLine("The standard galactic salary is...{0:C}", s.Salary);

    }

    class Employee
    {
        private string _name;
        private string _number;
        private int _date;
        private int _salary;
        public …
Run Code Online (Sandbox Code Playgroud)

c#

16
推荐指数
3
解决办法
5万
查看次数

我试图找出如何将罗马数字转换为整数

我试图弄清楚如何将罗马数字转换为整数.这是我代码的一部分.当我提示用户输入M时它显示1000,但是当我提示用户输入罗马数字(如VM)时,它不会给我995而是1005.这是因为我告诉我的程序这样做.

我想弄清楚的是我如何向前看并让它知道何时添加或减去罗马数字.

我该如何开始这样做?

class Roman
{

    public int inprogress = 0;
    public Roman(string roman)
    {

        char temp = 'Z';
        int length;

        length = roman.Length;

        for (int i = 0; i < length; i++)
        {
            temp = roman[i];
            if (temp == 'M')
            {
                inprogress = inprogress + 1000;
            }
            if (temp == 'D')
            {
                inprogress = inprogress + 500;
            }
            if (temp == 'C')
            {
                inprogress = inprogress + 100;
            }
            if (temp == 'L')
            {
                inprogress = inprogress + 50; …
Run Code Online (Sandbox Code Playgroud)

c# roman-numerals

6
推荐指数
1
解决办法
1252
查看次数

如何使更改计算器更简单

好的,所以一个月前上课,秋季学期开始在3个星期内恢复.我想在书中做我们从未分配的作业.这个问题让我陷入困境,因为它在第二章中.它是一个给予改变的程序(92美分是编译时初始化).我的问题是......有没有什么方法可以使这个程序更"虚拟"而不是它.而且我还必须把(int)放在我对aQuarter,anDime等的分配之前.否则我之后会得到小数.这是为什么?有经验的程序员能解释一下吗?

另外,本章讨论了MOD,这就是我使用它的原因.这是在引入类方法之前,显然是在循环和数组之前.所以我不能使用任何这些工具.应该只是一个非常基本的"洞穴人"计划......

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

namespace ConsoleApplication273
{
    class Program
    {
        static void Main(string[] args)
        {
        double change = 0.92;

        double quarter = 0.25;
        double dime = 0.10;
        double nickel = 0.05;
        double pennies = 0.01;

        double anQuarter = (int)(change / quarter);
        double anDime = (int)((change % quarter) / dime);
        double anNickel = (int)(((change % quarter) % dime) / nickel);
        double anPennies = (int)((((change % quarter) % dime) % nickel) / pennies);

        Console.WriteLine("The amount of …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
1万
查看次数

如何在c#中调用构造函数

好的.我想抓住施工人员.我想把这名员工命名为Stormtrooper.但它似乎没有称之为.我不知道我是不是正确地阅读了构造函数,或者我的内容是什么

任何帮助,将不胜感激.这是我意识到的一个非常noob的问题.

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

namespace ConsoleApplication297
{
public class Program
{
    static void Main(string[] args)
    {
        Employee s = new Employee(name);
        Console.WriteLine(s.Name);
        Console.ReadKey();
    }

    public class Employee
    {
        private string name;

        public string Name
        {
            get
            {
                return name;
            }
        }
        public Employee(string name)
        {
            name = Stormtrooper;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

c#

1
推荐指数
1
解决办法
112
查看次数

如何结束循环

开始学习循环.似乎这些事情永远存在,因为我并没有告诉它停止.问题是我不知道如何告诉它停止.我假设一个声明,如!=但我真的不知道.

有人在意解释循环如何停止?

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

 namespace ConsoleApplication326
 {
class Program
{
    static void Main(string[] args)
    {
        bool triLoop = true;
        while (triLoop)
        {
            Console.WriteLine("Please enter the first integer...");
            int firstInt = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the second integer...");
            int secondInt = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the third integer...");
            int thirdInt = int.Parse(Console.ReadLine());

            if ((firstInt + secondInt > thirdInt) && (secondInt + thirdInt > firstInt) && (firstInt + thirdInt > secondInt))
            {
                Console.WriteLine("The numbers {0}, {1}, and {2} …
Run Code Online (Sandbox Code Playgroud)

c# loops

1
推荐指数
1
解决办法
182
查看次数

仍然无法理解.ToString()

问题是"覆盖ToString()方法以返回所有数据成员.

当我返回_name时,会给出我在当前上下文中不存在的错误.我不明白为了返回所有数据成员应该如何看待

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication297
{
    class Program
    {
        static void Main(string[] args)
        {
            String name = "Stormtrooper";
            Employee s = new Employee(name);
            Console.WriteLine("The type of hire is a {0}", s.Name);
            Console.WriteLine("The identification number is {0}", s.Number);
            Console.WriteLine("The date of hire is {0} ABY", s.Date);
            Console.WriteLine("The standard galactic salary is...{0:C}", s.Salary);
        }

        class Employee
        {
            private string _name;
            private string _number;
            private int _date;
            private int _salary;

            public string Name
            {
                get
                { …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
146
查看次数

尝试使用方法添加,相信某些东西不是实例化的

当我输入文本框时,我希望它添加数字,相反,如果我输入(例如)12,再次点击存款,它只显示12.我认为这是因为它似乎认为它是0加12每次.有些东西似乎没有正确实例化.我认为.谁能指出我做错了什么?

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void btn_deposit_Click(object sender, EventArgs e)
    {
        double input;
        input = double.Parse((putin.Text));
        BankAccount a = new BankAccount(input);
        aMtBox.Text = a.AccountBalance.ToString();
    }
}

public class BankAccount
{
    private double num1;
    private double accountBalance;

    public BankAccount(double input)
    {
        num1 = input;
        Deposit();
    }

    public double Num1
    {
        set {num1 = value;}
        get {return num1;}}

    public double AccountBalance
    {
        get {return accountBalance;}
        set {accountBalance = value;}}

    public …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
58
查看次数

使用数组和链接列表 - 使用整数和字符串进行转换

所以我已经编程了大约6个月,因为它是休息,我很无聊,玩弄东西.我再次讨论了数组的概念并制作了一个数组,它产生了50个randoms数字,并使用插入排序对它们进行排序.然后我开始搞乱字符串数组.string[] array = new string[] { "Ultima Online", "Everquest", "Baldur's Gate", "Diablo I"};是我的字符串数组.为了让它显示整个字符串,我使用了一个链表(我不确定是否必须这样,我试图将其转换为无效,即Convert.ToString(数组)失败).所以我有这个

static void Main(string[] args)
    {
        string[] array = new string[] { "Ultima Online", "Everquest", "Baldur's Gate", "Diablo I"};
        List<string> list = new List<string>(array);

        for (int i = 0; i < list.Count; i++)
        {
            Console.WriteLine(list[i]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这将显示整个阵列.然后我想如果我想删除或添加一些内容.所以,我放在list.RemoveAt(2);我的上方Console.Writeline(list[i]);

但这确实是我没想到的.它会删除列表中第二个点之后的所有内容.所以我假设RemoveAt()将始终删除指示的值之后的所有内容.但是,list.Remove(2);给出了我的错误.1.参数1:无法从'int'转换为'string'然后2.最好的重载方法匹配... etc ..参数.我的演员再次无效.所以,我回到了RemoveAt,想了一下.出于某种原因,我想如果我使用等效的if语句,它会起作用,而且确实如此.

static void Main(string[] args)
    {
        string[] array = new string[] { "Ultima Online", "Everquest", "Baldur's Gate", "Diablo I" };
        List<string> …
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
1
解决办法
124
查看次数

标签 统计

c# ×8

loops ×1

roman-numerals ×1