Исключения
18.05.2021, 09:42. Показов 457. Ответов 1
Здраствуйте. Изначально было необходимо создать калькулятор для расчета фигур, что было сделано:
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| using System;
using static System.Math;
namespace Фигуры
{
interface IFigureInfo
{
double volume1(); double volume2();
}
class Sphere : IFigureInfo
{
double radius; // радиус шара
double diameter;// диамтер шара
public Sphere(double rad, double dia)
{ radius = rad;
diameter = dia;
}
public double volume1()
{
return Abs(PI * radius * radius * radius * 4 / 3);
}
public double volume2()
{
return Abs(PI * diameter * diameter * diameter * 1 / 6);
}
}
class Parallelepiped : IFigureInfo
{
double Length; // длина параллелепипеда
double Width; // ширина параллелепипеда
double Height; // высота параллелепипеда
public Parallelepiped(double ln, double wid, double hei)
{
Length = ln;
Width = wid;
Height = hei;
}
public double volume1()
{
return Abs(Length * Width * Height);
}
public double volume2() { return 1; }
}
class Pyramid : IFigureInfo
{
double Length; // высота пирамиды
double Area; // площадь пирамиды
public Pyramid(double len, double ar)
{
Length = len;
Area = ar;
}
public double volume1()
{
return Abs(Area * Length / 3);
}
public double volume2() { return 1; }
}
class Program
{
static public void InfoFigure(string fig, double v)
{
Console.WriteLine("{0} объем = {1:##.###}", fig, v);
}
public static void Main()
{
Console.WriteLine("Для какой из следующих фигур вы хотели бы рассчитать объем: параллелепипеда, шара, пирамиды");
Console.WriteLine("Введите 1 для шара, 2 для параллелепипеда, 3 для пирамиды");
string vshape = Console.ReadLine().ToLower();
if (vshape == ("1"))
{
Console.WriteLine("Введите 1 для диаметра, 2 для радиуса");
string choice = Console.ReadLine().ToLower();
if (choice == ("1"))
{ Console.Write("Диаметр шара = ");
double dia = Convert.ToDouble(Console.ReadLine());
Sphere v = new Sphere(0, dia);
InfoFigure("Шар: ", v.volume2());
}
if (choice == ("2"))
{
Console.Write("Радиус шара = ");
double rad = Convert.ToDouble(Console.ReadLine());
Sphere v = new Sphere(rad, 0);
InfoFigure("Шар: ", v.volume1());
}
}
if (vshape == ("2"))
{
Console.Write("Высота параллелепипеда = ");
double hei = Convert.ToDouble(Console.ReadLine());
Console.Write("Длина параллелепипеда = ");
double ln = Convert.ToDouble(Console.ReadLine());
Console.Write("Ширина параллелепипеда = ");
double wid = Convert.ToDouble(Console.ReadLine());
Parallelepiped n = new Parallelepiped(ln, wid, hei);
InfoFigure("Параллелепипед: ", n.volume1());
}
if (vshape == ("3"))
{
Console.Write("Высота пирамиды = ");
double len = Convert.ToDouble(Console.ReadLine());
Console.Write("Площадь пирамиды = ");
double ar = Convert.ToDouble(Console.ReadLine());
Pyramid x = new Pyramid(ar, len);
InfoFigure("Пирамида: ", x.volume1());
}
Console.ReadKey();
}
}
} |
|
После преподаватель предложил сделать исключения:
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
| using System;
using static System.Math;
namespace Фигуры
{
interface IFigureInfo
{
double volume1(); double volume2();
}
class Sphere : IFigureInfo
{
double radius; // радиус шара
double diameter;// диамтер шара
public Sphere(double rad, double dia)
{
radius = rad;
diameter = dia;
}
public double volume1()
{
return Abs(PI * radius * radius * radius * 4 / 3);
}
public double volume2()
{
return Abs(PI * diameter * diameter * diameter * 1 / 6);
}
}
class Parallelepiped : IFigureInfo
{
double Length; // длина параллелепипеда
double Width; // ширина параллелепипеда
double Height; // высота параллелепипеда
public Parallelepiped(double ln, double wid, double hei)
{
Length = ln;
Width = wid;
Height = hei;
}
public double volume1()
{
return Abs(Length * Width * Height);
}
public double volume2() { return 1; }
}
class Pyramid : IFigureInfo
{
double Length; // высота пирамиды
double Area; // площадь пирамиды
public Pyramid(double len, double ar)
{
Length = len;
Area = ar;
}
public double volume1()
{
return Abs(Area * Length / 3);
}
public double volume2() { return 1; }
}
class Program
{
static public double exception(double x)//исключения
{
if (x < 0.0) throw new ArithmeticException("Number must be positive or zero");
if (!double.TryParse(Console.ReadLine(), out x)) throw new ArgumentOutOfRangeException("Can't enter text");
else return x;
}
static public void InfoFigure(string fig, double v)//вывод занчений
{
Console.WriteLine("{0} объем = {1:##.###}", fig, v);
}
static public void Main()
{
Console.WriteLine("Для какой из следующих фигур вы хотели бы рассчитать объем: параллелепипеда, шара, пирамиды");
Console.WriteLine("Введите 1 для шара, 2 для параллелепипеда, 3 для пирамиды");
string vshape = Console.ReadLine().ToLower();
if (vshape == ("1"))
{
double x;
Console.WriteLine("Введите 1 для диаметра, 2 для радиуса");
string choice = Console.ReadLine().ToLower();
if (choice == ("1"))
{
Console.Write("Диаметр шара = ");
try {
x = Convert.ToDouble(Console.ReadLine());
exception(x);
Sphere v = new Sphere(0, x);
InfoFigure("Шар: ", v.volume2());
}
catch (ArithmeticException e)
{
Console.WriteLine("Число не должно быть отрицательным");
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("Нельзя вводить текст");
}
}
if (choice == ("2"))
{
Console.Write("Радиус шара = ");
try
{
x = Convert.ToDouble(Console.ReadLine());
exception(x);
Sphere m = new Sphere(x, 0);
InfoFigure("Шар: ", m.volume1());
}
catch (ArithmeticException e)
{
Console.WriteLine("Число не должно быть отрицательным");
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("Нельзя вводить текст");
}
}
}
if (vshape == ("2"))
{
double ln, wid, hei;
try
{
Console.Write("Высота параллелепипеда = ");
hei = Convert.ToDouble(Console.ReadLine());
exception(hei);
Console.Write("Длина параллелепипеда = ");
ln = Convert.ToDouble(Console.ReadLine());
exception(ln);
Console.Write("Ширина параллелепипеда = ");
wid = Convert.ToDouble(Console.ReadLine());
exception(wid);
Parallelepiped n = new Parallelepiped(ln, wid, hei);
InfoFigure("Параллелепипед: ", n.volume1());
}
catch (ArithmeticException e)
{
Console.WriteLine("Число не должно быть отрицательным");
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("Нельзя вводить текст");
}
}
if (vshape == ("3"))
{
double len, ar;
try
{
Console.Write("Высота пирамиды = ");
len = Convert.ToDouble(Console.ReadLine());
exception(len);
Console.Write("Площадь пирамиды = ");
ar = Convert.ToDouble(Console.ReadLine());
exception(ar);
Pyramid x = new Pyramid(ar, len);
InfoFigure("Пирамида: ", x.volume1());
}
catch (ArithmeticException e)
{
Console.WriteLine("Число не должно быть отрицательным");
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("Нельзя вводить текст");
}
}
Console.ReadKey();
}
}
} |
|
Код работает, но возникла проблема при вводе значений: после сообщения "Высота параллелепипеда = " необходимо вводить другое значение для продолжения ввода и если ввести буквы или просто нажать enter выходит сообщение об ошибке
Как исправить?
0
|