如何在可以更改值的位置添加选项? (Int.parse) [英] How do I add an option where I can change value? (Int.parse)

查看:62
本文介绍了如何在可以更改值的位置添加选项? (Int.parse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到了这个任务,要求我在名为Box的类中添加变量,用于框的宽度,长度和高度。然后在类中添加一个名为Insert()的选项,我可以通过在控制台中键入来更改值。

So i got this task which asks me to add variables in Class named "Box" for box wideness, length and height. And after that add an option called "Insert()" in the class which i can change values by typing in console.

class Box
{
    private int length, wideness, height;

    public Box(int boxlength, int boxwideness, int boxheight)
    {
        this.length = length;
        this.wideness = wideness;
        this.height =height;

    }
    public Insert()
    {
        length = int.Parse(Console.ReadLine());
        wideness = int.Parse(Console.ReadLine());
        height = int.Parse(Console.ReadLine());
    }


    public int TakeLength() { return length; }

    public int TakeWideness() { return wideness; }

    public int TakeHeight() { return height; }
}





我的尝试:



需要有关如何制作它的帮助,以便我可以插入值



What I have tried:

Need help on how to make it so that i can insert values

推荐答案

请问您的问题是什么?你的代码有效。但是你应该为你的代码adpat你的构造函数没有意义。



May I ask what your problem is? Your code works. But nonetheless you should adpat your constructor for your code makes no sense.

class Program
{
    static void Main(string[] args)
    {
        Box myBox = new Box(10, 20, 5);
        Console.WriteLine("Insert values for length, wideness and height: ");
        myBox.Insert();
        Console.WriteLine("Result: ");
        Console.WriteLine("Length: {0}", myBox.TakeLength());
        Console.WriteLine("Wideness: {0}", myBox.TakeWideness());
        Console.WriteLine("Height: {0}", myBox.TakeHeight());
        Console.WriteLine("Press any key to quit.");
        Console.ReadKey(true);
    }
}

class Box
{
    private int length, wideness, height;

    public Box(int boxlength, int boxwideness, int boxheight)
    {
        this.length = boxlength; // Assign input value!
        this.wideness = boxwideness; // Assign input value!
        this.height = boxheight; // Assign input value!
    }
    public void Insert()
    {
        Console.WriteLine("What length?");
        length = int.Parse(Console.ReadLine());
        Console.WriteLine("What wideness?");
        wideness = int.Parse(Console.ReadLine());
        Console.WriteLine("What height?");
        height = int.Parse(Console.ReadLine());
    }

    public int TakeLength() { return length; }

    public int TakeWideness() { return wideness; }

    public int TakeHeight() { return height; }
}





您输入第一个值并按Enter键,然后按第二个和第三个。然后你就完成了。



You enter first value and press enter, then second and third. And then you're done.


这篇关于如何在可以更改值的位置添加选项? (Int.parse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆