Skip to content

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Numbers #9

wants to merge 10 commits into from

Conversation

DanneZ98
Copy link

@DanneZ98 DanneZ98 commented Aug 28, 2020

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

Comment on lines +9 to +37
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++;
}
Copy link
Owner

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++)

Comment on lines +28 to +29
var input1 = Console.ReadLine();
var integer1 = Convert.ToInt32(input1);
Copy link
Owner

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);
Copy link
Owner

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);
Copy link
Owner

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('░');
}
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants