Skip to content

Instantly share code, notes, and snippets.

@AlexaJavaDev
Created June 17, 2026 14:58
Show Gist options
  • Select an option

  • Save AlexaJavaDev/5b3ea248054fef7abcc4a873a67888c4 to your computer and use it in GitHub Desktop.

Select an option

Save AlexaJavaDev/5b3ea248054fef7abcc4a873a67888c4 to your computer and use it in GitHub Desktop.
Класс Battery с проверкой границ заряда
/*
Вопрос: Как сделать так, чтобы при разряде или перезаряде
батарея автоматически ограничивалась значениями 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());
}
}
@AlexaJavaDev

Copy link
Copy Markdown
Author

Просто проверка))

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