-1

I am trying to limit the user from overfeeding a virtual cat by making it only if health level is less than 60 they can feed it.Also if user attempts to feed cat when health is over 60 then happiness decreases by 2 for option 1. I tried health < 60; but it gives me an error.This is different because it is limiting not comparing strings. different question not even sure why it's marked.

int food = 20;
int health = 30;
int happiness = 80;
String option;
System.out.println("pick an option ");
System.out.println(" Option 1 feed cat ");
System.out.println(" Option 2 play with cat");
System.out.println(" Option 3 give cat bath");
option = input.nextLine();
if(option == 1) {
    food += 10;
    happiness -= 15
    health < 60;
    happiness -=2;
} else if (option == 2) {
    happiness += 12;
    health += 25;
}
  • I think you mean `options = input.nextLine();` There is no variable `option` – Scary Wombat Oct 01 '19 at 02:58
  • @ScaryWombat I handtyped the code since it won't let me copy and paste but it's options on both the string and if else statement and I still can't limit the user by using health <60; – new_coder01 Oct 01 '19 at 03:01
  • I added a link showing how to compare Strings using Java – Scary Wombat Oct 01 '19 at 04:02
  • @ScaryWombat I changed it to a .equals() still doesn't help me limit the health which is my main question. – new_coder01 Oct 01 '19 at 04:43
  • Show us your attempt with `health < 60` condition, and also the error you receive – Lesiak Oct 01 '19 at 04:44
  • If you converted the `String` to an `int` [link](https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(java.lang.String)) then you could do numeric comparison. – Scary Wombat Oct 01 '19 at 04:54
  • @ScaryWombat I tried doing that it would highlight the option = input.nextLine (); in red and then I changed that to an int too but then it would highlight the .equals(); you suggested earlier – new_coder01 Oct 01 '19 at 05:00
  • If you are taking integer as a input don't use nextLine, use nextInt method. Or otherwise cast the string to integer by Integer.parseInt() – Shehan Dhaleesha Oct 01 '19 at 05:17

1 Answers1

0

Ok, first you need to convert a String to an int see https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(java.lang.String)

Also you are missing ; and what does health < 60 even mean?

Try this code

String option;
System.out.println("pick an option ");
System.out.println(" Option 1 feed cat ");
System.out.println(" Option 2 play with cat");
System.out.println(" Option 3 give cat bath");
option = input.nextLine();
if(Integer.valueOf(option) == 1) {

    if (health >= 60) {
        food += 10;
        happiness -= 15;  // where did this requirement come from?  Should it be +=?
    } else {
        happiness -=2;
    }
} else if (Integer.valueOf(option) == 2) {
    happiness += 12;
    health += 25;
}

Of course you should ensure that option can be converted to an int first

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64