Created
June 17, 2026 14:58
-
-
Save AlexaJavaDev/5b3ea248054fef7abcc4a873a67888c4 to your computer and use it in GitHub Desktop.
Класс Battery с проверкой границ заряда
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Вопрос: Как сделать так, чтобы при разряде или перезаряде | |
| батарея автоматически ограничивалась значениями 0 и 100? | |
| */ | |
| class Battery { | |
| int charge = 100; | |
| public void drain(int amount) { | |
| charge -= amount; | |
| } | |
| public void charge(int amount) { | |
| charge += amount; | |
| } | |
| public String getCharge() { | |
| if (charge < 0) { | |
| return "Батарея разряжена!"; | |
| } else if (charge > 100) { | |
| return "Батарея заряжена!"; | |
| } else { | |
| return "Заряд: " + charge; | |
| } | |
| } | |
| } | |
| public class BatteryMain { | |
| public static void main(String[] args) { | |
| Battery battery = new Battery(); | |
| battery.drain(75); | |
| System.out.println(battery.getCharge()); | |
| battery.charge(70); | |
| System.out.println(battery.getCharge()); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Просто проверка))