@@ -159,30 +159,30 @@ Let's continue by adding a more complex set of calculator code to your project.
159
159
{
160
160
static void Main (string [] args )
161
161
{
162
- // Declare variables and then initialize to zero
162
+ // Declare variables and then initialize to zero.
163
163
int num1 = 0 ; int num2 = 0 ;
164
164
165
- // Display title as the C# console calculator app
165
+ // Display title as the C# console calculator app.
166
166
Console .WriteLine (" Console Calculator in C#\r " );
167
167
Console .WriteLine (" ------------------------\n " );
168
168
169
- // Ask the user to type the first number
169
+ // Ask the user to type the first number.
170
170
Console .WriteLine (" Type a number, and then press Enter" );
171
171
num1 = Convert .ToInt32 (Console .ReadLine ());
172
172
173
- // Ask the user to type the second number
173
+ // Ask the user to type the second number.
174
174
Console .WriteLine (" Type another number, and then press Enter" );
175
175
num2 = Convert .ToInt32 (Console .ReadLine ());
176
176
177
- // Ask the user to choose an option
177
+ // Ask the user to choose an option.
178
178
Console .WriteLine (" Choose an option from the following list:" );
179
179
Console .WriteLine (" \t a - Add" );
180
180
Console .WriteLine (" \t s - Subtract" );
181
181
Console .WriteLine (" \t m - Multiply" );
182
182
Console .WriteLine (" \t d - Divide" );
183
183
Console .Write (" Your option? " );
184
184
185
- // Use a switch statement to do the math
185
+ // Use a switch statement to do the math.
186
186
switch (Console .ReadLine ())
187
187
{
188
188
case " a" :
@@ -198,7 +198,7 @@ Let's continue by adding a more complex set of calculator code to your project.
198
198
Console .WriteLine ($" Your result: {num1 } / {num2 } = " + (num1 / num2 ));
199
199
break ;
200
200
}
201
- // Wait for the user to respond before closing
201
+ // Wait for the user to respond before closing.
202
202
Console .Write (" Press any key to close the Calculator console app..." );
203
203
Console .ReadKey ();
204
204
}
@@ -282,7 +282,7 @@ Let's change the code to handle this error.
282
282
1 . Replace it with the following code :
283
283
284
284
```csharp
285
- // Ask the user to enter a non-zero divisor until they do so
285
+ // Ask the user to enter a non-zero divisor until they do so.
286
286
while (num2 == 0 )
287
287
{
288
288
Console .WriteLine (" Enter a non-zero divisor: " );
@@ -311,9 +311,9 @@ To fix this error, we must refactor the code that we've previously entered.
311
311
312
312
#### Revise the code
313
313
314
- Rather than rely on the `program` class to handle all the code, we'll divide our app into two classes: `calculator ` and `program `.
314
+ Rather than rely on the `program` class to handle all the code, we'll divide our app into two classes: `Calculator ` and `Program `.
315
315
316
- The `calculator ` class will handle the bulk of the calculation work, and the `program ` class will handle the user interface and error-capturing work.
316
+ The `Calculator ` class will handle the bulk of the calculation work, and the `Program ` class will handle the user interface and error-capturing work.
317
317
318
318
Let's get started.
319
319
@@ -328,16 +328,16 @@ Let's get started.
328
328
329
329
```
330
330
331
- 1 . Next , add a new `calculator ` class , as follows :
331
+ 1 . Next , add a new `Calculator ` class , as follows :
332
332
333
333
```csharp
334
334
class Calculator
335
335
{
336
336
public static double DoOperation (double num1 , double num2 , string op )
337
337
{
338
- double result = double .NaN ; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error
338
+ double result = double .NaN ; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.
339
339
340
- // Use a switch statement to do the math
340
+ // Use a switch statement to do the math.
341
341
switch (op )
342
342
{
343
343
case " a" :
@@ -350,13 +350,13 @@ Let's get started.
350
350
result = num1 * num2 ;
351
351
break ;
352
352
case " d" :
353
- // Ask the user to enter a non-zero divisor
353
+ // Ask the user to enter a non-zero divisor.
354
354
if (num2 != 0 )
355
355
{
356
356
result = num1 / num2 ;
357
357
}
358
358
break ;
359
- // Return text for an incorrect option entry
359
+ // Return text for an incorrect option entry.
360
360
default :
361
361
break ;
362
362
}
@@ -366,26 +366,26 @@ Let's get started.
366
366
367
367
```
368
368
369
- 1 . Then , add a new `program ` class , as follows :
369
+ 1 . Then , add a new `Program ` class , as follows :
370
370
371
371
```csharp
372
372
class Program
373
373
{
374
374
static void Main (string [] args )
375
375
{
376
376
bool endApp = false ;
377
- // Display title as the C# console calculator app
377
+ // Display title as the C# console calculator app.
378
378
Console .WriteLine (" Console Calculator in C#\r " );
379
379
Console .WriteLine (" ------------------------\n " );
380
380
381
381
while (! endApp )
382
382
{
383
- // Declare variables and set to empty
383
+ // Declare variables and set to empty.
384
384
string numInput1 = " " ;
385
385
string numInput2 = " " ;
386
386
double result = 0 ;
387
387
388
- // Ask the user to type the first number
388
+ // Ask the user to type the first number.
389
389
Console .Write (" Type a number, and then press Enter: " );
390
390
numInput1 = Console .ReadLine ();
391
391
@@ -396,7 +396,7 @@ Let's get started.
396
396
numInput1 = Console .ReadLine ();
397
397
}
398
398
399
- // Ask the user to type the second number
399
+ // Ask the user to type the second number.
400
400
Console .Write (" Type another number, and then press Enter: " );
401
401
numInput2 = Console .ReadLine ();
402
402
@@ -407,7 +407,7 @@ Let's get started.
407
407
numInput2 = Console .ReadLine ();
408
408
}
409
409
410
- // Ask the user to choose an operator
410
+ // Ask the user to choose an operator.
411
411
Console .WriteLine (" Choose an operator from the following list:" );
412
412
Console .WriteLine (" \t a - Add" );
413
413
Console .WriteLine (" \t s - Subtract" );
@@ -433,11 +433,11 @@ Let's get started.
433
433
434
434
Console .WriteLine (" ------------------------\n " );
435
435
436
- // Wait for the user to respond before closing
436
+ // Wait for the user to respond before closing.
437
437
Console .Write (" Press 'n' and Enter to close the app, or press any other key and Enter to continue: " );
438
438
if (Console .ReadLine () == " n" ) endApp = true ;
439
439
440
- Console .WriteLine (" \n " ); // Friendly linespacing
440
+ Console .WriteLine (" \n " ); // Friendly linespacing.
441
441
}
442
442
return ;
443
443
}
@@ -480,9 +480,9 @@ namespace Calculator
480
480
{
481
481
public static double DoOperation (double num1 , double num2 , string op )
482
482
{
483
- double result = double .NaN ; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error
483
+ double result = double .NaN ; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.
484
484
485
- // Use a switch statement to do the math
485
+ // Use a switch statement to do the math.
486
486
switch (op )
487
487
{
488
488
case " a" :
@@ -495,13 +495,13 @@ namespace Calculator
495
495
result = num1 * num2 ;
496
496
break ;
497
497
case " d" :
498
- // Ask the user to enter a non-zero divisor
498
+ // Ask the user to enter a non-zero divisor.
499
499
if (num2 != 0 )
500
500
{
501
501
result = num1 / num2 ;
502
502
}
503
503
break ;
504
- // Return text for an incorrect option entry
504
+ // Return text for an incorrect option entry.
505
505
default :
506
506
break ;
507
507
}
@@ -514,18 +514,18 @@ namespace Calculator
514
514
static void Main (string [] args )
515
515
{
516
516
bool endApp = false ;
517
- // Display title as the C# console calculator app
517
+ // Display title as the C# console calculator app.
518
518
Console .WriteLine (" Console Calculator in C#\r " );
519
519
Console .WriteLine (" ------------------------\n " );
520
520
521
521
while (! endApp )
522
522
{
523
- // Declare variables and set to empty
523
+ // Declare variables and set to empty.
524
524
string numInput1 = " " ;
525
525
string numInput2 = " " ;
526
526
double result = 0 ;
527
527
528
- // Ask the user to type the first number
528
+ // Ask the user to type the first number.
529
529
Console .Write (" Type a number, and then press Enter: " );
530
530
numInput1 = Console .ReadLine ();
531
531
@@ -536,7 +536,7 @@ namespace Calculator
536
536
numInput1 = Console .ReadLine ();
537
537
}
538
538
539
- // Ask the user to type the second number
539
+ // Ask the user to type the second number.
540
540
Console .Write (" Type another number, and then press Enter: " );
541
541
numInput2 = Console .ReadLine ();
542
542
@@ -547,7 +547,7 @@ namespace Calculator
547
547
numInput2 = Console .ReadLine ();
548
548
}
549
549
550
- // Ask the user to choose an operator
550
+ // Ask the user to choose an operator.
551
551
Console .WriteLine (" Choose an operator from the following list:" );
552
552
Console .WriteLine (" \t a - Add" );
553
553
Console .WriteLine (" \t s - Subtract" );
@@ -573,11 +573,11 @@ namespace Calculator
573
573
574
574
Console .WriteLine (" ------------------------\n " );
575
575
576
- // Wait for the user to respond before closing
576
+ // Wait for the user to respond before closing.
577
577
Console .Write (" Press 'n' and Enter to close the app, or press any other key and Enter to continue: " );
578
578
if (Console .ReadLine () == " n" ) endApp = true ;
579
579
580
- Console .WriteLine (" \n " ); // Friendly linespacing
580
+ Console .WriteLine (" \n " ); // Friendly linespacing.
581
581
}
582
582
return ;
583
583
}
0 commit comments