import java.util.*;
public class Homework4 {
static String name; // declare variable name to String type
static boolean check = false; // declare variable check to boolean type and assign it to false
static int age, currentHunger, currentDirtiness, currentBoredom, hungerRate, dirtyRate, boredomRate, playerChoice; // declare variables to integer type
static final int LOW_STAT_THRESHOLD = 10;
static final int MED_STAT_THRESHOLD = 20;
static final int HIGH_STAT_THRESHOLD = 30;
static final int RUNAWAY_STAT_THRESHOLD = 40;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
createPet();
while (!check) { // "while" loop runs until the user loses (while check will not "true")
ageOfPet();
choice(); // call the necessary methods to display the game properly
startStatusOfPet();
runAwayCheck();
}
resultOfPlayer();
}
public static void createPet() {
age = 0; // initialize age variable to 0
System.out.println("A mysterious egg hatches before you!\nWhat will you name this creature?"); // prompt to the user
name = input.nextLine(); // user should enter pet name
currentHunger = 10;
currentDirtiness = 10; // current stats of pet
currentBoredom = 10;
hungerRate = 10;
dirtyRate = 10; // step of changing values
boredomRate = 10;
}
public static void ageOfPet() { // method for changing age of the pet each loop
age++; // incrementing age by 1 each loop
System.out.println("*********BIRTHDAY UPDATE!*********");
System.out.println(name + " is now " + age + " days old!");
if (age % 2 == 0) { // if age divisible by 2 without remainder hungerRate will increase by 1
hungerRate += 1;
System.out.println(name + " gets hungrier faster!");
} else if (age % 3 == 0) { // if age divisible by 3 without remainder dirtyRate will increase by 1
dirtyRate += 1;
System.out.println(name + " gets dirtier faster!");
} else if (age % 5 == 0) { // if age divisible by 5 without remainder boredomRate will increase by 1
boredomRate += 1;
System.out.println(name + " gets more boring!");
}
}
public static void choice() { // method for for options for the user to choose what to do with the pet
System.out.println("1.) Feed\n2.) Clean\n3.) Play"); // options for the user what to do
System.out.println("What would you like to do with " + name + "?");
playerChoice = input.nextInt();
if (playerChoice == 1)
statusAfterEating();
else if (playerChoice == 2)
statusAfterCleaning();
else
statusAfterPlaying();
}
private static void statusAfterEating() { // method for changing values if pet is eating
currentHunger -= hungerRate;
currentDirtiness += dirtyRate;
System.out.println("You feed " + name + " but it makes a mess...");
}
private static void statusAfterCleaning() { // method for changing values if pet is cleaning
currentDirtiness -= dirtyRate;
currentBoredom += boredomRate;
System.out.println("You bathe " + name + " but if doesn't like it...");
}
private static void statusAfterPlaying() { // method for changing values if pet is playing
currentBoredom -= boredomRate;
currentHunger += hungerRate;
System.out.println("You play with " + name + " and it works up an appetite!");
}
public static void startStatusOfPet() { // method to show level of hunger, cleanliness and boredom
System.out.println("*********CURRENT STATS*********");
String hungerDescription = "Hunger level: ";
System.out.println(hungerDescription + statusPet());
String dirtDescription = "Cleanliness level: ";
System.out.println(dirtDescription + statusPet());
String boredDescription = "Boredom level: ";
System.out.println(boredDescription + statusPet());
}
public static String statusPet() { // method with multiple if...else statements which will determine the level hunger, cleanliness and boredom
if ((currentHunger >= LOW_STAT_THRESHOLD) && (currentHunger < MED_STAT_THRESHOLD))
return "Pretty good!";
else if ((currentDirtiness >= LOW_STAT_THRESHOLD) && (currentDirtiness < MED_STAT_THRESHOLD))
return "Pretty good!";
else if ((currentBoredom >= LOW_STAT_THRESHOLD) && (currentBoredom < MED_STAT_THRESHOLD))
return "Pretty good!";
if ((currentHunger >= MED_STAT_THRESHOLD) && (currentHunger < HIGH_STAT_THRESHOLD))
return "Could be better...";
else if ((currentDirtiness >= MED_STAT_THRESHOLD) && (currentDirtiness < HIGH_STAT_THRESHOLD))
return "Could be better...";
else if ((currentBoredom >= MED_STAT_THRESHOLD) && (currentBoredom < HIGH_STAT_THRESHOLD))
return "Could be better...";
if ((currentHunger >= HIGH_STAT_THRESHOLD) && (currentHunger < RUNAWAY_STAT_THRESHOLD))
return "Not good!";
else if ((currentDirtiness >= HIGH_STAT_THRESHOLD) && (currentDirtiness < RUNAWAY_STAT_THRESHOLD))
return "Not good!";
else if ((currentBoredom >= HIGH_STAT_THRESHOLD) && (currentBoredom < RUNAWAY_STAT_THRESHOLD))
return "Not good!";
if ((currentHunger == RUNAWAY_STAT_THRESHOLD) || (currentDirtiness == RUNAWAY_STAT_THRESHOLD) || (currentBoredom == RUNAWAY_STAT_THRESHOLD))
return "The pet has serious problem!";
return "";
}
public static void runAwayCheck() { // method with if statement which will determine if the pet has run away
if (currentHunger > RUNAWAY_STAT_THRESHOLD || currentDirtiness > RUNAWAY_STAT_THRESHOLD || currentBoredom > RUNAWAY_STAT_THRESHOLD) {
System.out.println("The pet ran away because it was too hungry!");
check = true; // variable check becomes true, therefore while loop will be finished
}
}
public static void resultOfPlayer() { // method will show the result of the user at the end of the game when the pet runs away and shows to user is a good owner or not
System.out.println("You took care of " + name + " for " + age + " days");
if (age < 5)
System.out.println("Try to be better next time.");
else if (age >= 6 && age <= 15)
System.out.println("You spent many days with " + name + "!");
else if (age >= 16 && age <= 25)
System.out.println("You are great virtual pet owner!");
else if (age > 25)
System.out.println("Great! You are ready to take care of real pet!");
}
}
Ivan Zelenkov
28 уровень
Почему уровень голода, чистоты и скуки у питомца не меняется отдельно, а одновременно. ПОМОГИТЕ ПОЖАЛУЙСТА
Обсуждается
Комментарии (3)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
Ksenia VolkovaJava Developer в DXCMaster
30 сентября 2020, 17:41
Что значит "не меняется отдельно"? А как он должен меняться?
0
Ivan Zelenkov
30 сентября 2020, 18:47
Это неправильно
Hunger level: Pretty good!
Cleanliness: Pretty good!
Boredom level: Pretty good!
Так должно быть:
Hunger level: Pretty good!
Cleanliness: Not good!
Boredom level: Could be better!
0
Ksenia VolkovaJava Developer в DXCMaster
30 сентября 2020, 19:44
А это уже проблема твоего метода statusPet()
0