-
Notifications
You must be signed in to change notification settings - Fork 34
Numbers #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Numbers #9
Conversation
var integerValues = new[] { 1, 2, 3 }; | ||
var name = nameof(integerValues); | ||
Console.WriteLine("For-loop"); | ||
for (var i = 0; i < integerValues.Length; i++) | ||
{ | ||
var value = integerValues[i]; | ||
|
||
Console.WriteLine($"Index {i} i arrayen {name} har värdet: {value}"); | ||
} | ||
Console.WriteLine(); | ||
Console.WriteLine("Do-while-loop"); | ||
var doWhileIndex = 0; | ||
do | ||
{ | ||
var value = integerValues[doWhileIndex]; | ||
Console.WriteLine($"Index {doWhileIndex} i arrayen {name} har värdet: {value}"); | ||
doWhileIndex++; | ||
} | ||
while (doWhileIndex < integerValues.Length); | ||
|
||
var whileIndex = 0; | ||
Console.WriteLine(); | ||
Console.WriteLine("While-loop"); | ||
while(whileIndex < integerValues.Length) | ||
{ | ||
var value = integerValues[whileIndex]; | ||
Console.WriteLine($"Index {whileIndex} i arrayen {name} har värdet: {value}"); | ||
whileIndex++; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Denna kod skulle må bra av lite tomma rader. Prova att lägga in en före och efter varje Console.Write som ligger utanför kodblock.
var integerValues = new[] { 1, 2, 3 };
var name = nameof(integerValues);
Console.WriteLine("For-loop");
for (var i = 0; i < integerValues.Length; i++)
var input1 = Console.ReadLine(); | ||
var integer1 = Convert.ToInt32(input1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Här skulle man helt enkelt kunna återanvända input
och integer
utan att skapa nya variabler.
input = Console.ReadLine();
integer = Convert.ToInt32(input);
{ | ||
Console.WriteLine("Värdet är " + number); | ||
|
||
testValue = Convert.ToInt32(number); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variabelnamnet testvalue
blir lite generellt i detta hänseende, fundera på om det finns något ännu tydligare.
minValue = testValue; | ||
} | ||
|
||
total += Convert.ToInt32(number); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Här konverteras number
till int
ytterligare en gång (som på rad 28).
För att undvika ineffektivitet i koden och att man behöver genomföra ändringar på flera ställen så nyttjar vi på Geta DRY-principen. Här kan man läsa mer om den.
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
Console.Write('▓'); | ||
else | ||
Console.Write('░'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bra lösning på problemet!
Se ovan om DRY-principen.
I wrote this number app as a part of an exercise for school. This is my proposed solution for the problem. Don't know why there is a bunch of them but 'ändringar' is the finished project