Статическая переменная count не уменьшается
Никак не получается сделать так, чтоб уменьшалось количество котов. В консоли выдаёт постоянно -overbignumber. Count нужно добавить именно в метод meow. В getStatus сказали не добавлять.
import javax.sound.midi.Soundbank;
public class Cat
{
private double originWeight;
private double weight;
private double minWeight;
private double maxWeight;
private double eatenFood = 0;
private static int count = 0;
public Cat()
{
count++;
weight = 1500.0 + 3000.0 * Math.random();
originWeight = weight;
minWeight = 1000.0;
maxWeight = 9000.0;
}
public void meow()
{
weight = weight - 1;
if (weight < minWeight || weight > maxWeight);
count = count - 1;
}
public void feed(double amount)
{
weight = weight + amount;
eatenFood += amount;
eatenFood++;
if (weight < minWeight || weight > maxWeight);
count = count - 1;
}
public double getEatenFood()
{
return eatenFood;
}
public void drink(Double amount)
{
if (weight < minWeight || weight > maxWeight);
count = count - 1;
weight = weight + amount;
}
public double getWeight()
{
return weight;
}
public String getStatus()
{
if(weight < minWeight) {
return "Dead";
}
else if(weight > maxWeight) {
return "Exploded";
}
else if(weight > originWeight) {
return "Sleeping";
}
else {
return "Playing";
}
}
// сходить в туалет!
public void pee () {
weight = weight - 100.0;
System.out.println("I do it!");
if (weight < minWeight || weight > maxWeight);
count = count - 1;
}
public static int getCount()
{
return count;
}
}
//// Другой класс
public class Loader {
public static void main(String[] args) {
{
System.out.println("Cats count:" + Cat.getCount());
Cat murka = new Cat();
Cat vasya = new Cat();
Cat rings = new Cat();
Cat masha = new Cat();
Cat mashka = new Cat();
System.out.println("Cats count" + Cat.getCount());
while (murka.getWeight() > 1000)
murka.meow();
while (masha.getWeight() > 1000)
masha.meow();
vasya.feed(vasya.getWeight()/0.1);
rings.feed(rings.getWeight()/0.1);
System.out.println("Murka status:" + murka.getStatus());
System.out.println("Vasya status:" + vasya.getStatus());
System.out.println("Masha status:" + masha.getStatus());
System.out.println("Rings status:" + rings.getStatus());
System.out.println("Cats count:" + Cat.getCount());
}
}
}```
Сделав count в getStatus всегда выходило нужное количество котов, когда они умирали/взрывались. Сейчас же, если убрать
while (masha.getWeight() > 1000)
masha.meow();
то тоже выходит нужное количество котов. Но с этим методом вечно минус тысяча котов.
Ответы (1 шт):
Автор решения: Юлия
→ Ссылка
Ну вот примерно как-то так...
if (weight < minWeight || weight > maxWeight){
count = count - 1;
}