Skip to content

Instantly share code, notes, and snippets.

@RimuruDev
Last active December 7, 2022 15:14
Show Gist options
  • Save RimuruDev/16d9af09e0bd39a9568ba7dc7c0e8fbd to your computer and use it in GitHub Desktop.
Save RimuruDev/16d9af09e0bd39a9568ba7dc7c0e8fbd to your computer and use it in GitHub Desktop.
Помог Nikolum с олимпиадной задачей. [Формула -> (a+b)*2+4] задача в комментариях
/* Это плохой пример, так как если ввести букву вместо числа, программа упадет.
* length - Это буква "a" из формулы
* width - Это буква "b" из формулы
*
*/
using System;
namespace Wire_length
{
internal class WireLength
{
private static void Main()
{
Console.Write("Введите длину проволоки: ");
int length = int.Parse(Console.ReadLine());
Console.Write("Введите ширину проволоки: ");
int width = int.Parse(Console.ReadLine());
int result = (length + width) * 2 + 4;
Console.WriteLine($"Длина проволоки: {result}");
Console.ReadKey();
}
}
}
/* Это хороший пример, так как если ввести букву вместо числа, программа не упадет.
* length - Это буква "a" из формулы
* width - Это буква "b" из формулы
*
*/
using System;
namespace Wire_length
{
internal class WireLength
{
private static void Main()
{
int length = 0;
int width = 0;
Console.Write("Введите длину проволоки: ");
if (int.TryParse(Console.ReadLine(), out int inputLength))
{
length = inputLength;
}
else
{
Console.WriteLine("Вы ввели неверные данные! Пожалуйста введите корректное целочисленное значение.");
return;
}
Console.Write("Введите ширину проволоки: ");
if (int.TryParse(Console.ReadLine(), out int inputWidth))
{
width = inputWidth;
}
else
{
Console.WriteLine("Вы ввели неверные данные! Пожалуйста введите корректное целочисленное значение.");
return;
}
int result = (length + width) * 2 + 4;
Console.WriteLine($"Длина проволоки: {result}");
Console.ReadKey();
}
}
}
/* Это идеальный пример, со всеми обработками.
* length - Это буква "a" из формулы
* width - Это буква "b" из формулы
*/
using System;
namespace Wire_length
{
internal class WireLength
{
private static void Main()
{
int length = UserInput("Введите длину проволоки: ");
int width = UserInput("Введите ширину проволоки: ");
int result = (length + width) * 2 + 4;
Console.WriteLine($"Длина проволоки = {result}\n");
}
private static int UserInput(string message)
{
Console.Write(message);
if (int.TryParse(Console.ReadLine(), out int inputLength))
{
return inputLength;
}
else
{
Console.WriteLine("Вы ввели неверные данные! Пожалуйста введите корректное целочисленное значение.");
return UserInput(message);
}
}
}
}
/* Это идеальный пример, со всеми обработками.
* length - Это буква "a" из формулы
* width - Это буква "b" из формулы
*/
using System;
namespace Wire_length
{
internal class WireLength
{
private static void Main()
{
double length = UserInput("Введите длину проволоки: ");
double width = UserInput("Введите ширину проволоки: ");
double result = (length + width) * 2 + 4;
Console.WriteLine($"Длина проволоки = {result}\n");
}
private static double UserInput(string message)
{
Console.Write(message);
if (double.TryParse(Console.ReadLine(), out double inputLength))
{
return inputLength;
}
else
{
Console.WriteLine("Вы ввели неверные данные! Пожалуйста введите корректное число. Пример: 12,1 или 17");
return UserInput(message);
}
}
}
}
/* Это идеальный пример, со всеми обработками.
* length - Это буква "a" из формулы
* width - Это буква "b" из формулы
*/
using System;
// https://gist.github.com/RimuruDev/16d9af09e0bd39a9568ba7dc7c0e8fbd
namespace Wire_length
{
internal class WireLength
{
private static void Main()
{
double length = UserInput("Введите длину проволоки: ");
double width = UserInput("Введите ширину проволоки: ");
double result = (length + width) * 2 + 4;
Console.WriteLine($"Длина проволоки = {result}\n");
}
private static double UserInput(string message)
{
Console.Write(message);
if (double.TryParse(Console.ReadLine(), out double inputLength))
{
if (inputLength > 0)
{
return inputLength;
}
else
{
Console.WriteLine("Число не может быть меньше 0");
return UserInput(message);
}
}
else
{
Console.WriteLine("Вы ввели неверные данные! Пожалуйста введите корректное число. Пример: 12,1 или 17");
return UserInput(message);
}
}
}
}
/* Это идеальный пример, со всеми обработками.
* length - Это буква "a" из формулы
* width - Это буква "b" из формулы
*/
using System;
// https://gist.github.com/RimuruDev/16d9af09e0bd39a9568ba7dc7c0e8fbd
namespace Wire_length
{
internal class WireLength
{
private static void Main()
{
double length = UserInput("Введите длину проволоки: ");
double width = UserInput("Введите ширину проволоки: ");
double result = (length + width) * 2 + 4;
Console.WriteLine($"Длина проволоки = {result}\n");
}
private static double UserInput(string message)
{
Console.Write(message);
if (double.TryParse(Console.ReadLine(), out double inputLength))
{
if (inputLength > 0)
{
return inputLength;
}
else
{
return Error(message, "Число не может быть меньше 0");
}
}
else
{
return Error(message, "Вы ввели неверные данные! Пожалуйста введите корректное число. Пример: 12,1 или 17");
}
}
private static double Error(string message, string errorMessage)
{
Console.WriteLine(errorMessage);
return UserInput(message);
}
}
}
@RimuruDev
Copy link
Author

изображение
изображение
изображение

@RimuruDev
Copy link
Author

изображение

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment