[英]trying to create a simple Accessors program
我有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; }
}
您正在執行以下操作:
Console.WriteLine("Total is : ", total);
// In userInput.total()
應該用以下方法更改:
Console.WriteLine("Total is : " + total);
OR
Console.WriteLine("Total is : {0}", total);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.