[英]trying to create a simple Accessors program
I have 3 classes and one struct and I am trying to print out the total using accessors. 我有3个类和一个结构,我正在尝试使用访问器打印出总数。 The program prints the message the total is .
程序打印总计为的消息。 No result is printed.
没有结果打印。 trying to figure this out?
试图找出答案?
Main: 主要:
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();
}
}
Program class: 程序类别:
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);
}
}
user input: 用户输入:
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();
}
}
total: 总:
struct Total
{
public int total { get; set; }
}
You are doing like: 您正在执行以下操作:
Console.WriteLine("Total is : ", total);
// In userInput.total()
It should be changed with: 应该用以下方法更改:
Console.WriteLine("Total is : " + total);
OR
Console.WriteLine("Total is : {0}", total);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.