Fix UTF-8 encoding and add null-safety parsing for inputs

This commit is contained in:
2026-09-09 09:54:53 +03:00
parent 13882ab7c2
commit 59cd1bce36
3 changed files with 16 additions and 18 deletions
+3 -2
View File
@@ -1,4 +1,5 @@
using System; using System;
namespace Lab01 namespace Lab01
{ {
internal class Program internal class Program
@@ -37,4 +38,4 @@ namespace Lab01
Console.WriteLine("****************************************"); Console.WriteLine("****************************************");
} }
} }
} }
+8 -7
View File
@@ -1,4 +1,5 @@
using System; using System;
namespace Lab02 namespace Lab02
{ {
internal class Program internal class Program
@@ -10,10 +11,10 @@ namespace Lab02
// Задание 1.1: Расчёт характеристик покемона // Задание 1.1: Расчёт характеристик покемона
Console.WriteLine("=== Прокачка покемона ==="); Console.WriteLine("=== Прокачка покемона ===");
Console.Write("Введите базовое значение атаки: "); Console.Write("Введите базовое значение атаки: ");
int baseAttack = Convert.ToInt32(Console.ReadLine()); int.TryParse(Console.ReadLine(), out int baseAttack);
Console.Write("Введите количество полученных уровней: "); Console.Write("Введите количество полученных уровней: ");
int levels = Convert.ToInt32(Console.ReadLine()); int.TryParse(Console.ReadLine(), out int levels);
int totalAttack = baseAttack + (levels * 5); int totalAttack = baseAttack + (levels * 5);
Console.WriteLine($"Итоговая атака: {totalAttack}\n"); Console.WriteLine($"Итоговая атака: {totalAttack}\n");
@@ -24,7 +25,7 @@ namespace Lab02
Console.WriteLine($"Текущий баланс: {balance} монет."); Console.WriteLine($"Текущий баланс: {balance} монет.");
Console.Write("Сколько зелий лечения вы хотите купить? "); Console.Write("Сколько зелий лечения вы хотите купить? ");
int potionCount = Convert.ToInt32(Console.ReadLine()); int.TryParse(Console.ReadLine(), out int potionCount);
int totalPrice = potionCount * potionPrice; int totalPrice = potionCount * potionPrice;
balance -= totalPrice; balance -= totalPrice;
@@ -35,7 +36,7 @@ namespace Lab02
// Задание 2.1: Проверка состояния покемона в бою // Задание 2.1: Проверка состояния покемона в бою
Console.Write("Введите текущее количество HP покемона: "); Console.Write("Введите текущее количество HP покемона: ");
int currentHp = Convert.ToInt32(Console.ReadLine()); int.TryParse(Console.ReadLine(), out int currentHp);
if (currentHp <= 0) if (currentHp <= 0)
{ {
@@ -64,7 +65,7 @@ namespace Lab02
Console.WriteLine("4 — Побег"); Console.WriteLine("4 — Побег");
Console.Write("Выберите действие (1-4): "); Console.Write("Выберите действие (1-4): ");
int action = Convert.ToInt32(Console.ReadLine()); int.TryParse(Console.ReadLine(), out int action);
switch (action) switch (action)
{ {
@@ -86,4 +87,4 @@ namespace Lab02
} }
} }
} }
} }
+5 -9
View File
@@ -1,17 +1,13 @@
fun main() { fun main() {
// --- ЗАДАНИЕ 1. Явное приведение типов (Casting) --- // --- ЗАДАНИЕ 1. Явное приведение типов (Casting) ---
print("Введите номер текущего хода: ") print("Введите номер текущего хода: ")
val currentTurnInput = readln() val currentTurn = readln().toIntOrNull() ?: 1
print("Введите количество энергии на столе: ") print("Введите количество энергии на столе: ")
val energyInput = readln() val energy = readln().toIntOrNull() ?: 0
print("Введите коэффициент усиления атаки: ") print("Введите коэффициент усиления атаки: ")
val multiplierInput = readln() val multiplier = readln().toDoubleOrNull() ?: 1.0
val currentTurn: Int = currentTurnInput.toInt()
val energy: Int = energyInput.toInt()
val multiplier: Double = multiplierInput.toDouble()
println("\n=== ИНИЦИАЛИЗАЦИЯ ПАРАМЕТРОВ ХОДА ===") println("\n=== ИНИЦИАЛИЗАЦИЯ ПАРАМЕТРОВ ХОДА ===")
println("Текущий ход: $currentTurn (тип Int)") println("Текущий ход: $currentTurn (тип Int)")
@@ -27,13 +23,13 @@
println() println()
print("Введите базовый урон покемона: ") print("Введите базовый урон покемона: ")
val baseDamage = readln().toInt() val baseDamage = readln().toIntOrNull() ?: 0
println("Применена карта Тренера 'Леди из Центра Покемонов'!") println("Применена карта Тренера 'Леди из Центра Покемонов'!")
cardsPlayed += 1 cardsPlayed += 1
println() println()
print("Сколько карт энергии прикрепить к Покемону? ") print("Сколько карт энергии прикрепить к Покемону? ")
val energyAttached = readln().toInt() val energyAttached = readln().toIntOrNull() ?: 0
cardsPlayed += energyAttached cardsPlayed += energyAttached
val finalDamage = baseDamage * multiplier val finalDamage = baseDamage * multiplier