且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

任何人都可以帮我完成这个C#任务吗?

更新时间:2023-11-20 11:38:52

让我们看看..



1。创建一个控制台应用程序。

看起来你做到了。你的文件中是否有一个你没有在这里发帖的命名空间?



2。添加一个名为compute_product的公共类。

你做到了。



3。在compute_product中声明了两个名为value1和value2的私有字段(类变量)。

你做了那样 - 但如果你的老师很挑剔我宁愿把字段命名为他想要的字段你来。



4.对于字段(类变量)value2创建一个名为Value2的公共属性并实现get和set。


现在它变得有趣了。您是否认识到作业中字段和属性名称之间的区别?一个是小写,一个是大写。现在你明显倾向于修改名字会让你感到害怕。在C#中,小写和大写有所不同。如果它们存在于同一范围内,则将两个事物(字段,属性,方法,类等)命名为完全相同的错误。

为其setter中的属性的后备字段分配一个常量值(10)绝对没有意义。 setter用于指定从外部传递给它的值。



5。通过在Main中实例化构造函数时将整数值传递给构造函数,添加一个构造函数来为value1字段赋值。

你还没有这样做。



6。添加名为public void display_product_calc()的方法,并使用Console.WriteLine显示将value1 * Value2乘以控制台的结果。

您添加了方法。

修复第4点之后,你还需要在这里修复乘法表达式。

你试图返回结果而不是将它显示到控制台。除此之外,你不能从该方法返回任何东西,因为它被声明为 void



7。在Main中声明一个名为myValue1的变量,并为其赋值5.

除了 cp 之外,你没有声明任何变量在 Main()

cp.val1 是私有字段,你可以' t在课外 compute_product 分配给它。



8。在Main中实例化一个名为myproduct的compute_product对象,并在构造函数参数列表中传递myValue1。

您实例化了一个 compute_product 对象(但又一次)用不同的名字命名。

你没有把任何东西传递给它的构造函数(见第5点)。



9。在Main中使用Value2类属性将类变量value2的值设置为整数10.

val2 在 Main()。

修复第4点后你可以在这里正确地做到这一点。



10。从Main调用compute_product类的display_product_calc方法;显示的结果将是50。

显示是Main()中的未知标识符(但即使它被声明,它也是不会那样工作,你可能忘了那里的加号)。

控制台输出不应出现在 Main()中,而应出现在 display_product_calc()中。


在这里,我感谢你的支持人员,

并感谢对我如此吝啬,它帮助了我很多







公共类compute_product

{

private int value1;

private int value2;

public int Value2

{

get

{

返回值2;

}

设置

{

value2 = value;

}



}

public compute_product(int value)

{

value1 = value;

}



public void display_product_calc()

{



Console.WriteLine(产品是+ value1 * value2);

}

static void Main(string [] args)

{

int myValue1 = 5;

compute_product myproduct = new compute_product(myValue1);

myproduct.value1 = myValue1;

myproduct.Value2 = 10;

myproduct.display_product_calc();

}

}

1. Create a console application.
2. Add a public class called compute_product.
3. In compute_product declare two private fields (class variables) named value1 and value2.
4. For field (class variable) value2 create a public property named Value2 and implement the get and set.
5. Add a constructor to assign a value to the value1 field by passing an integer value to the constructor when the constructor is instantiated in Main.
6. Add a method named public void display_product_calc() and use a Console.WriteLine to display the result of multiplying value1* Value2 to the console.
7. In Main declare a variable named myValue1 and assign the value 5 to it.
8. In Main instantiate a compute_product object named myproduct and pass myValue1 in the constructor parameter list.
9. In Main use the Value2 class property to set the value of class variable value2 to integer 10.
10. Call the compute_product class display_product_calc method from Main; the displayed result will be 50.

The output would look like this:

The product is 50
Press any key to continue . . .

What I have tried:

class Program

{
  
  static void Main()
  
  {
     
       
 compute_product cp=new compute_product();
       
 cp.val1=5;
val2=10;

display_product_calc();
Console.WriteLine("The product of two numbers is:\n" display);
      
   
 }

}

public class compute_product
{

    private int val1;

    private int val2;

    public int val2
    {

        get
        {

            return val2;

        }

        set
        {

            val2 = 10;

        }

    }


    public void display_product_calc()
    {
        int result;
        result = (val1 * val2);
        return result;
    }
}

Let's see..

1. Create a console application.
Looks like you did that. Is there a namespace in your file which you just didn't post here?

2. Add a public class called compute_product.
You did that.

3. In compute_product declare two private fields (class variables) named value1 and value2.
You sort of did that - but if your teacher is picky I'd rather name the fields exactly like he wanted you to.

4. For field (class variable) value2 create a public property named Value2 and implement the get and set.

Now it get's interesting. Did you recognize the difference between the field and property name in the assignment? One is lower case, one is upper case. And now your apparent tendency to modify names bites you. In C#, lower case and upper case make a difference. Naming two "things" (fields, properties, methods, classes, ...) exactly the same is an error if they exist within the same scope.
Assigning a constant value (10) to the backing field of a property in its setter makes absolutely no sense. The setter exists to assign a value that gets passed to it from "outside".

5. Add a constructor to assign a value to the value1 field by passing an integer value to the constructor when the constructor is instantiated in Main.
You didn't do that yet.

6. Add a method named public void display_product_calc() and use a Console.WriteLine to display the result of multiplying value1* Value2 to the console.
You added the method.
After fixing point 4) you also need to fix your multiplication expression here.
You attempt to return the result instead of displaying it to the console. Apart from that, you can't return anything from that method because it's declared as void.

7. In Main declare a variable named myValue1 and assign the value 5 to it.
You didn't declare any variable other than cp in Main().
cp.val1 is a private field, you can't assign to it outside of the class compute_product.

8. In Main instantiate a compute_product object named myproduct and pass myValue1 in the constructor parameter list.
You instantiated a compute_product object (but again named it differently).
You didn't pass anything into it's constructor (see point 5).

9. In Main use the Value2 class property to set the value of class variable value2 to integer 10.
val2 is unknown in Main().
After fixing point 4) you can do this properly here.

10. Call the compute_product class display_product_calc method from Main; the displayed result will be 50.
display is an unknown identifier in Main() (but even if it was declared, it wouldn't work like that, you probably forgot a plus sign there).
The console output should not happen in Main() but in display_product_calc().


Here I done it thank for your support guys,
and thanks for being so mean to me, it helped me alot



public class compute_product
{
private int value1;
private int value2;
public int Value2
{
get
{
return value2;
}
set
{
value2 = value;
}

}
public compute_product(int value)
{
value1 = value;
}

public void display_product_calc()
{

Console.WriteLine("The product is" + value1 * value2);
}
static void Main(string[] args)
{
int myValue1 = 5;
compute_product myproduct = new compute_product(myValue1);
myproduct.value1 = myValue1;
myproduct.Value2 = 10;
myproduct.display_product_calc();
}
}