1)
程序代码:
using System;
namespace Console01
{
class Program
{
static void Main()
{
int x;
int y;
while (true)
{
Console.Write("Input x:");
var xString = Console.ReadLine();
if (!int.TryParse(xString, out x)) continue;
break;
}
while (true)
{
Console.Write("Input y:");
var yString = Console.ReadLine();
if (!int.TryParse(yString, out y)) continue;
break;
}
var class0 = new Class0 { x = x, y = y };
Console.WriteLine(string.Concat("Variable x in class : ", class0.x));
Console.WriteLine(string.Concat("Variable y in class : ", class0.y));
Console.Read();
}
public class Class0
{
public int x;
public int y;
}
}
}
2)
程序代码:
using System;
namespace Console02
{
class Program
{
static void Main()
{
int x;
int y;
while (true)
{
Console.Write("Input x:");
var xString = Console.ReadLine();
if (!int.TryParse(xString, out x)) continue;
break;
}
while (true)
{
Console.Write("Input y:");
var yString = Console.ReadLine();
if (!int.TryParse(yString, out y)) continue;
break;
}
var class0 = new Class0(x, y);
Console.Read();
}
public class Class0
{
private int x;
private int y;
public Class0(int varX, int varY)
{
x = varX;
y = varY;
Console.WriteLine(string.Concat("Variable x : ", x));
Console.WriteLine(string.Concat("Variable y : ", y));
}
}
}
}
3)
程序代码:
using System;
namespace Console03
{
class Program
{
static void Main()
{
int x;
int y;
while (true)
{
Console.Write("Input x:");
var xString = Console.ReadLine();
if (!int.TryParse(xString, out x)) continue;
break;
}
while (true)
{
Console.Write("Input y:");
var yString = Console.ReadLine();
if (!int.TryParse(yString, out y)) continue;
break;
}
var class0 = new Class0 { X = x, Y = y };
Console.WriteLine(string.Concat("Variable x in class : ", class0.X));
Console.WriteLine(string.Concat("Variable y in class : ", class0.Y));
Console.Read();
}
private class Class0
{
private int x;
private int y;
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}
}
}
4)
程序代码:
using System;
namespace Console04
{
class Program
{
static void Main()
{
var myClassk = new MyClassk();
Console.WriteLine(string.Concat("a : ", myClassk.a));
Console.WriteLine(string.Concat("b : ", myClassk.B));
Console.WriteLine(string.Concat("c : ", myClassk.c));
Console.WriteLine(string.Concat("d : ", myClassk.D));
Console.WriteLine(string.Concat("e : ", myClassk.E));
Console.Read();
}
public class MyClassk
{
public int a;
private string b;
internal int c;
private static int d;
private readonly int e;
public MyClassk()
{
b = "题目怎么没说要赋值呀?";
c = int.MinValue;
d = 0;
e = int.MaxValue;
}
public string B { get { return b; } }
public int D { get { return d; } }
public int E { get { return e; } }
}
}
}
这是老师布置的作业吧?没学过但觉得挺好玩的,挺羡慕你的。