我有3个类和一个结构,我正在尝试使用访问器打印出总数。程序打印总数为的消息。没有打印结果。 试图解决这个问题?
主:
class MainClass
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("1. option one");
list.Add("2. option two");
list.Add("3. option three");
list.Add("4. option four");
list.Add("5. option five");
Console.WriteLine("1. option one");
Console.WriteLine("2. option two");
Console.WriteLine("3. option three");
Console.WriteLine("4. option four");
Console.WriteLine("5. option five");
Console.WriteLine(" Pick 3 options ");
UserInput usi = new UserInput();
usi.Get();
Console.WriteLine(" You Picked ");
usi.print();
Console.ReadLine();
}
}
计划类:
class Program
{
private int A, B;
public void GetAB()
{
Console.WriteLine("Enter A: ");
A = int.Parse(Console.ReadLine());
if ((A <= 0) || (A >= 6))
// Console.WriteLine("good");
throw new ArgumentOutOfRangeException("Number must be between 1-5");
Console.WriteLine("Enter B: ");
B = int.Parse(Console.ReadLine());
if ((B <= 0) ||(B >=6))
throw new ArgumentOutOfRangeException("Number must be between 1-5");
}
public void PrintAB()
{
Console.WriteLine("A = {0}\tb = {1}\t", A, B);
}
}
用户输入:
class UserInput : Program
{
int C;
public void Get()
{
try
{
Console.WriteLine("Valid entries are 1-5");
GetAB();
Console.Write("Enter C: ");
C = int.Parse((Console.ReadLine()));
if ((C <= 0) || (C >= 6))
throw new ArgumentOutOfRangeException("Number must be between 1-5");
}
catch (ArgumentOutOfRangeException ex)
{
// Show the user that 7 cannot be divided by 2.
Console.WriteLine("Error: {0}", ex.Message);
Console.WriteLine("Please try again ");
System.Environment.Exit(1);
}
}
public int A
{ get; set; }
public int B
{ get; set; }
public void total()
{
int total = (A + B + C);
Console.WriteLine(" Total is ", total);
}
public void print()
{
PrintAB();
Console.WriteLine("C = {0}", C);
total();
}
}
总:
struct Total
{
public int total { get; set; }
}
答案 0 :(得分:1)
你这样做:
Console.WriteLine("Total is : ", total);
// In userInput.total()
应该改为:
Console.WriteLine("Total is : " + total);
OR
Console.WriteLine("Total is : {0}", total);