我正在接受编程课的介绍,并且遇到了作业问题。该代码应该计算一个值,但我总是得到的答案是0.不知道我做错了什么,但希望有人能帮助我,并向我解释我的错误在哪里? 初学者的任何提示?计算值始终为0,我不知道为什么
class changeValue
{
//Create a class called changeValue that declares 2 integer class variables: value1 and
// value2. These should be declared as public and you should not use automatic properties
// to declare them.
private int _value1;
private int _value2;
public int Value1
{
get
{
return _value1;
}//end get
set
{
_value1 = value;
}//end set
}
public int Value2
{
get
{
return _value2;
}//end get
set
{
_value2 = value;
}//end set
}
public changeValue(int val1, int val2)
{
//here is the constructor where you code the if statements
int value1 = val1;
int value2 = val2;
if (value1 > 5)
{
value1 = val1;
}
if (val1 <= 5)
{
value1 = (val1+val2);
}
if (val2 < 10)
{
value2 = (val2 * val2 + 5);
}
if (val2 >= 10)
{
value2 = val2;
}
}
public void printit()
{
//here is the printit method used to print the results
Console.WriteLine("The calculated value is:" + (Value1 * Value2));
}
}
class assignment3
{
public static void Main(string[] args)
{
//declare the local val1 and val2 integer variables
int val1;
int val2;
//prompt the user for input of two integers
//don’t forget to convert from the string input to integer
Console.Write("Enter an integer value: "); //obtain user input
val1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a second integer value: "); //obtain user input
val2 = Convert.ToInt32(Console.ReadLine());
//instantiate a changeValue object here
changeValue myValue = new changeValue(val1,val2);
myValue.printit();//call the object method printit here
}
}
}
感谢所有这种帮助,这个类是不容易的。
你试图* *调试**吗? – Carsten 2014-09-12 16:33:26
你使用什么输入?你能给我们任何其他信息吗? – BradleyDotNET 2014-09-12 16:35:09
您从不将任何内容设置为Value1和Value2属性或其私有字段。您只是将值分配给构造函数中的私有字段。 – gmiley 2014-09-12 16:35:16