avnc 014
This commit is contained in:
parent
505772685e
commit
311341df0b
@ -313,4 +313,82 @@ them (for all potential code execution paths).
|
||||
|
||||
---
|
||||
|
||||
##
|
||||
## Exercise
|
||||
|
||||
### Complete a challenge activity using variable scope
|
||||
|
||||
Code challenges will reinforce what you've learned and help you gain some
|
||||
confidence before continuing on.
|
||||
|
||||
### Variable scope challenge
|
||||
|
||||
In this challenge, you'll use what you've learned about code blocks and
|
||||
variable scope to fix the poorly written code sample provided. There are many
|
||||
improvements that you can make. Good luck!
|
||||
|
||||
### Code challenge: update problematic code in the code editor
|
||||
|
||||
Ensure that you have an empty Program.cs file open.
|
||||
|
||||
Type the following code into the editor:
|
||||
|
||||
```cs
|
||||
int[] numbers = { 4, 8, 15, 16, 23, 42 };
|
||||
|
||||
foreach (int number in numbers) {
|
||||
int total;
|
||||
|
||||
total += number;
|
||||
|
||||
if (number == 42) {
|
||||
bool found = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
Console.WriteLine("Set contains 42");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Total: {total}");
|
||||
```
|
||||
|
||||
Review the intended output.
|
||||
|
||||
When you have completed the required code edits, your application should
|
||||
produce the following output:
|
||||
|
||||
```txt
|
||||
Set contains 42
|
||||
Total: 108
|
||||
```
|
||||
|
||||
Complete the code updates required so that the existing `Console.WriteLine()`
|
||||
statements produce the desired output.
|
||||
|
||||
Variable scope may need to be adjusted.
|
||||
|
||||
Optimize your code for readability.
|
||||
|
||||
Depending on the amount of whitespace that you include and some other factors,
|
||||
you should have around 17 lines of code.
|
||||
|
||||
```cs
|
||||
int[] numbers = { 4, 8, 15, 16, 23, 42 };
|
||||
int total = 0;
|
||||
bool found = false;
|
||||
|
||||
foreach (int number in numbers) {
|
||||
total += number;
|
||||
if (number == 42) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
Console.WriteLine("Set contains 42");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Total: {total}");
|
||||
```
|
||||
---
|
||||
|
@ -43,3 +43,46 @@ if (my_var1 > 0) {
|
||||
my_var1 += my_var2;
|
||||
}
|
||||
Console.WriteLine(my_var1);
|
||||
Console.WriteLine("----------");
|
||||
int[] numbers = { 4, 8, 15, 16, 23, 42 };
|
||||
int total=0;
|
||||
bool found=false;
|
||||
|
||||
foreach (int number in numbers) {
|
||||
total += number;
|
||||
if (number == 42) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
Console.WriteLine("Set contains 42");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Total: {total}");
|
||||
|
||||
Console.WriteLine("----------");
|
||||
int pepe = 1;
|
||||
|
||||
if (pepe > 0)
|
||||
Console.WriteLine($"IF: {pepe}");
|
||||
else if (pepe < 0)
|
||||
Console.WriteLine($"ELSE IF: {pepe}");
|
||||
else
|
||||
Console.WriteLine($"Else: {pepe}");
|
||||
|
||||
int mivar = 1;
|
||||
if (mivar>0){
|
||||
int mivar2 = 8;
|
||||
mivar += mivar2;
|
||||
}
|
||||
Console.WriteLine(mivar);
|
||||
|
||||
//int var1 = 5;
|
||||
//if (var1 > 0){
|
||||
// int var2 = 6;
|
||||
//}
|
||||
//var1 += var2;
|
||||
//Console.WriteLine(var1);
|
||||
|
||||
Console.WriteLine("----------");
|
||||
|
Loading…
Reference in New Issue
Block a user