Rock Paper Scissors Lizard Spock - java

My assignment:
Create a tester that creates two HandGamePlayer objects (default constructor for cpu full constructor for human), play for 3 rounds by asking the user for a choice of sign (1-5 now), outputting the two players signs, and outputting who won. Once all rounds are done, output the stats and declare the overall winner!
Been working on this all day, can anyone explain to me how to get the sign symbol to recognize? My professor tried to explain to me what to do but I'm just lost...
This is what he said to do, but I don't even know what it means:
"sign is not declared within main, so it says that the symbol can’t be found. You generated a hand sign, but printed it out and didn’t store it! Instead make an int sign; variable and store the hand sign there (still print it out, but now you’ll be able to use it in your multi-way if/else)"
Check out my code and let me know if you guys got any ideas.
HandGameTester.java
//MAIN
import java.util.Scanner;
public class HandGameTester
{
public static void main(String[] args)
{
Scanner keyboard;
HandSign hs;
int sign;
int handSign;
int win = 0;
int loose = 0;
int tied = 0;
int loopCount;
keyboard = new Scanner(System.in);
hs = new HandSign();
hs = sign;
System.out.println("Pick A Choice Below To Play");
System.out.println("1: Rock");
System.out.println("2: Paper");
System.out.println("3: Scissors");
System.out.println("4: Lizard");
System.out.println("5: Spock");
handSign = keyboard.nextInt();
System.out.println("You chose " + handSign);
hs.printHandSign(hs.getHandSign());
if(handSign == sign) {
System.out.println("You tied!");
tied++;
loopCount++;
} else if(handsign == 1 && sign == 2 || sign == 5) {
System.out.println("You loose!");
loose++;
loopCount++;
} else if(handsign == 1 && sign == 3 || sign == 4) {
System.out.println("You win!");
win++;
loopCount++;
} else if(handsign == 2 && sign == 1 || sign == 5) {
System.out.println("You win!");
win++;
loopCount++;
} else if(handsign == 2 && sign == 3 || sign == 2) {
System.out.println("You loose!");
loose++;
loopCount++;
} else if(handsign == 3 && sign == 2 || sign == 4) {
System.out.println("You win!");
win++;
loopCount++;
} else if(handsign == 3 && sign == 3 || sign == 5) {
System.out.println("You loose!");
loose++;
loopCount++;
} else if(handsign == 4 && sign == 2 || sign == 5) {
System.out.println("You win!");
win++;
loopCount++;
} else if(handsign == 4 && sign == 3 || sign == 4) {
System.out.println("You loose!");
loose++;
loopCount++;
} else if(handsign == 5 && sign == 1 || sign == 3) {
System.out.println("You win!");
win++;
loopCount++;
} else if(handsign == 5 && sign == 2 || sign == 4) {
System.out.println("You loose!");
loose++;
loopCount++;
} else {
System.out.println("You win!");
win++;
loopCount++;
} if((win > loose) || (win > tied)) {
System.out.println("You Win Best Out of Three!");
} else if(tied > win || tied > loose) {
System.out.println("You Tied!");
} else if(loose > win || loose > win) {
System.out.println("You loose!");
} else if(win == loose || win == tied) {
System.out.println("You tied!");
} else {
System.out.println("You Win Best Out of Three!");
}
}
}
HandSign.java
public class HandSign
{
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSOR = 3;
public static final int LIZARD = 4;
public static final int SPOCK = 5;
public static int getHandSign();
{
return (int)Math.random() * 5;
}
public static int printHandSign(int sign)
{
switch(sign)
{
case 1:
System.out.println("Rock");
return sign;
case 2:
System.out.println("Paper");
return sign;
case 3:
System.out.println("Scissor");
return sign;
case 4:
System.out.println("Lizard");
return sign;
case 5:
System.out.println("Spock");
return sign;
default:
System.out.println("Fatal Error");
System.exit(0);
}
}
}
}

There are numerous errors (but mostly unecessary variables) in your code.
I suppose you can tell your professor that he or she was wrong in that sign was, in fact, declared, but it was not initialized. Examine the beginning of your main() method. One line simply states,
int sign;
This is a declaration -- you are telling the program that the sign variable exists. However, you are not setting it equal to anything -- you are not initializing it.
However, this is certainly not to say that your code is without other flaws.
Firstly, as stated, you never initialized sign. As you later set handSign equal to the nextInt of a System.in Scanner, we can assume that handSign is the user's chosen sign (Either rock, paper, scissors, lizard, or spok.) You then compare handSign with sign and state that, if they are equal to one another, then you tied with the computer. By this we can assume that sign is the computer's chosen sign. When your professor told you that you did not store a variable within sign, he was dead right! You declared sign, but never made the computer choose what sign was -- you never set it equal to anything.
Look at your HandSign class. You created the getHandSign() method which returns a random value, but you never called the method!
Secondly, you have a lot of unecessary variables.
Here's what you want to do:
In the main method, get rid of the hs variable. Why? Think about it -- there are only two players, and so there will only be two signs. You have three variables for signs -- hs, handSign, and sign. You then imply that handSign is the user's sign and sign is the computer's sign in your if statements. hs is not only unecessary, but it is implied that a sign is, in your program, typically an Integer while hs is of the type HandSign. Also, every variable and method within the HandSign class is static, meaning that any instance of the HandSign class is, in essence, useless.
Secondly, you created several constants within your HandSign class, but never used them. You could either get rid of them entirely, or substitute the numbers in the switch statement for the constants. For example, where it says:
case 1:
you could (and should) change it to:
case ROCK:
You could (and should) do the same thing in your several if / else statements in the HandGameTester main() method. (Note that you would have to reference the constants via the HandSign class -- HandSign.ROCK, etc.)
Finally, and most importantly, initialize the sign variable!
You created the static method which generates a random value between 1 and 5, but never called it, so call it:
int sign = HandSign.getHandSign();
This will call the getHandSign() method and set sign equal to the returned random value between 1 and 5.

"sign is not declared within main, so it says that the symbol can’t be found.
It looks like you added this with
int sign;
You generated a hand sign, but printed it out and didn’t store it! Instead make an int sign; variable and store the hand sign there (still print it out, but now you’ll be able to use it in your multi-way if/else)"
What this means is that you have two HandSign variables, one that you generate (hs) and another you are reading in from the user (sign). However, sign is an int and hs isn't. So you need to get an int from HandSign by calling HandSign.getHandSign() to get the one from the computer player.

You have never remembered the sign generated by getHandSign. You call
hs.printHandSign(hs.getHandSign());
Which prints out the sign the computer generated, but you never remembered this sign in a variable.
You appear to have have a variable for this you just never give it a value.
int sign; // defaults to zero as you have not given it a value
...
hs = new HandSign();
hs = sign; // This line wont compile.
...
if(handSign == sign) { // sign still has no value set so is still the default of zero.
You should be assigning a value to sign before doing your if...else if.... That assignment should look something like
sign = ????;
With the question marks replaced by a valid integer expression.

Some other posters have commented but I'll just add my 2 cents. There are a few problems I can see immediately.
There is no loop in main (unless I'm hallucinating) which means that
only one round will be played.
It would be nice for user interface if you printed out the computer's choice.
You've misspelled "lose" as "loose" which makes the code harder to
read.
Blank lines appear in the wrong places, or not at all, which also
makes the code hard to read and debug.
Huge problem: (int)Math.random() * 5 doesn't do what you want it to
do. Just create a small program that prints the result of that
calculation 10 times and you'll see. You need more parentheses, and also
you need to add 1 because you started counting at 1 instead of 0.
Alternatively, you could start counting at 0 instead, which (IMHO) is
better.
You exit with a fatal error with System.exit(0) but if it really is
an error the argument should be > 0, e.g., System.exit(1).
In addition, I agree with the other posters. However, it shouldn't be too hard to fix, really just a few minutes. Most of the hard work is done.

Related

How to make an if statement with multiple variables and multiple conditions into a switch case ? Can && compare cases?

I'm making a rock paper scissors game for school and I have a working game but its 213 lines long. I was trying to shorten it up some by adding switch case in place of my if statements. Are there some instances where a switch case just won't work and it has to be an if statement?
String weaponChoice = keyboard.nextLine();
switch (weaponChoice) {
case "Rock":
System.out.println("You Chose Rock");
break;
case "Paper":
System.out.println("You Chose Paper");
break;
case "Scissors":
System.out.println("You chose Scissors");
break;
default:
System.out.println(weaponChoice + " is not a valid answer the computer gets a point!");
compScore++;
break;
}
I had the above code in an if statement and switched it over no problem but the below code I'm not sure how to change it over.
String getComputerChoiceVariable = getComputerChoice();
System.out.println("The computer chose " + getComputerChoiceVariable);
if (weaponChoice.equals(getComputerChoiceVariable)) {
System.out.println("It's a draw!");
ties++;
} else if ((weaponChoice.equals("Rock")) && (getComputerChoiceVariable.equals("Scissors"))) {
System.out.println("You won!");
userScore++;
} else if ((weaponChoice.equals("Paper")) && (getComputerChoiceVariable.equals("Rock"))) {
System.out.println("You won!");
userScore++;
} else if ((weaponChoice.equals("Scissors")) && (getComputerChoiceVariable.equals("Paper"))) {
System.out.println("You won!");
userScore++;
} else if (weaponChoice.equals("Rock") && getComputerChoiceVariable.equals("Paper")) {
System.out.println("You Lost!");
compScore++;
} else if (weaponChoice.equals("Paper") && getComputerChoiceVariable.equals("Scissors")) {
System.out.println("You Lost!");
compScore++;
} else if (weaponChoice.equals("Scissors") && getComputerChoiceVariable.equals("Rock")) {
System.out.println("You Lost!");
compScore++;
}
Maybe something like
switch (weaponChoice,getComputerChoiceVariable){
case "Rock",case "Scissors":
System.out.println("You won!");
}
But Java doesn't like that I get a ton of errors. Can a switch take two parameters?
I'm super new to Java.
Could I somehow use the && to compare cases?
A switch statement can test the value of only one expression. It is possible to nest switch statements, but that's overly verbose and is no better than a series of if/else statements.
As an aside, you may want to validate the user's weapon of choice, so someone doesn't choose Lizard, Spock, Foo, or something else unexpected.
You can concatenate the strings into one so it can be tested. I would use a separator such as "|" to make the choices clear. Also, different cases have the same logic, avoiding repetition.
switch(weaponChoice + "|" + computerChoice)
{
case "Rock|Scissors":
case "Scissors|Paper":
case "Paper|Rock":
System.out.println("You won!");
userScore++;
break;
case "Rock|Paper":
case "Paper|Scissors":
case "Scissors|Rock":
System.out.println("You lost!");
compScore++;
break;
default:
System.out.println("It's a draw!");
ties++;
}
This is concise and it lists all similar cases together, without repetition.
A switch block cannot be written with two parameters. From Oracle's documentation:
A switch works with the byte, short, char, and int primitive data
types. It also works with enumerated types (discussed in Enum Types),
the String class, and a few special classes that wrap certain
primitive types: Character, Byte, Short, and Integer (discussed in
Numbers and Strings).
However, you could make your program more modular by writing:
if (weaponChoice.equals(getComputerChoiceVariable)) {
System.out.println("It's a draw!");
ties++;
} else if (userWins(weaponChoice, getComputerChoiceVariable)) {
System.out.println("You won!");
userScore++;
} else {
System.out.println("You Lost!");
compScore++;
}
and using something like:
public boolean userWins(String userWeapon, String compWeapon) {
if((userWeapon.equals("Rock")) && (compWeapon.equals("Scissors"))) {
return true;
} else if((userWeapon.equals("Paper")) && (compWeapon.equals("Rock"))){
return true;
} else if((userWeapon.equals("Scissors")) && (compWeapon.equals("Paper"))){
return true;
} else if(userWeapon.equals("Rock") && compWeapon.equals("Paper")) {
return false;
} else if(userWeapon.equals("Paper") && compWeapon.equals("Scissors")) {
return false;
} else {
return false;
}
}
You don't necessarily need to make a switch of your code to shorten it. You can put the tree options in a list, and compare the user's choice with the value that that comes after the computer's choice to see if the user won.
If you have the list [Rock, Paper, Scissors], the computer chose Rock. Find the element after it, so Paper in this case. Now you only have to see if that value is equal to the user's choice, and if that's the case, the user won.
List<String> choices = Arrays.asList("Rock", "Paper", "Scissors");
if (playerChoice.equals(computerChoice)) {
System.out.println("Draw!");
} else if (playerChoice.equals(choices.get((choices.indexOf(computerChoice) + 1) % 3))) {
System.out.println("You won!");
} else {
System.out.println("You lost!");
}
Here choices.indexOf(computerChoice) finds the current positions of the computer's choice in the list. It add's one to find the next element in the list and uses the remainder operator (% 3) to make sure that if the next index is higher than the last element in the list, it flips back to the first element.

Checking if a user input that should be an int is a string

I'm trying to make this Sentinel program more robust by continuing it even when incorrect user inputs are received. I've gotten it to work if the user input is a different int, but if it is a string, or anything else really, the program crashes.
My current code attempt is this:
} else if (userInt != 1 && userInt != 2 && userInt != 3 && userInt != 4 && userInt != 5 && userInt !=6
|| userInt instanceof String) {
The first part of this code works fine at checking if the user input is a different in. The instanceof statement gives the error of "incompatible operand types int and String"
Should I even be using an instanceof statement? Is there a better way to check for this?
This is the whole method:
public static void printMenu() {
Scanner userInput2 = new Scanner(System.in);
String menu = new String(" Please choose from the following menu: \n 1. Rock paper Scissors\n 2. "
+ "Tip Calculator\n 3. "
+ "Number Adding\n 4. Guessing Game\n 5. Random\n 6. Exit");
System.out.println(menu);
int userInt = userInput2.nextInt();
if (userInt == 1) {
System.out.println(" You asked to play Rock Paper Scissors");
System.out.println(" Launching Rock Paper Scissors... \n");
RockPaperScissors gameRun1 = new RockPaperScissors();
gameRun1.main(null);
} else if (userInt == 2) {
System.out.println(" You asked to run the Tip Calculator");
System.out.println(" Launching the Tip Calculator... \n");
TipCalculator gameRun2 = new TipCalculator();
gameRun2.main(null);
} else if (userInt == 3) {
System.out.println(" You asked to run the Number Adding game");
System.out.println(" Launching the Number Adding game... \n");
NumberAddingGame gameRun3 = new NumberAddingGame();
gameRun3.main(null);
} else if (userInt == 4) {
System.out.println(" You asked to play GuessingGame");
System.out.println(" Launching GuessingGame... \n");
GuessingGame gameRun4 = new GuessingGame();
gameRun4.main(null);
} else if (userInt == 5) {
System.out.println(" You asked for a random game");
option5();
} else if (userInt == 6) {
System.out.println( "Thank you for using Conner's Sentinel");
// figure out how to terminate the program from here
} else if (userInt != 1 && userInt != 2 && userInt != 3 && userInt != 4 && userInt != 5 && userInt !=6
|| userInt instanceof String {
System.out.println("Not a valid input, type 1-6");
printMenu();
}
printMenu();
}
There is no way to check if the next input was an int like you are doing (userInput2.nextInt() can only return an int), instead you have to check before you assign the result. Something like,
if (userInput2.hasNextInt()) {
int userInt = userInput2.nextInt();
if (userInt == 1) {
System.out.println(" You asked to play Rock Paper Scissors");
System.out.println(" Launching Rock Paper Scissors... \n");
RockPaperScissors gameRun1 = new RockPaperScissors();
gameRun1.main(null);
} else if (userInt == 2) {
System.out.println(" You asked to run the Tip Calculator");
System.out.println(" Launching the Tip Calculator... \n");
TipCalculator gameRun2 = new TipCalculator();
gameRun2.main(null);
} else if (userInt == 3) {
System.out.println(" You asked to run the Number Adding game");
System.out.println(" Launching the Number Adding game... \n");
NumberAddingGame gameRun3 = new NumberAddingGame();
gameRun3.main(null);
} else if (userInt == 4) {
System.out.println(" You asked to play GuessingGame");
System.out.println(" Launching GuessingGame... \n");
GuessingGame gameRun4 = new GuessingGame();
gameRun4.main(null);
} else if (userInt == 5) {
System.out.println(" You asked for a random game");
option5();
} else if (userInt == 6) {
System.out.println("Thank you for using Conner's Sentinel");
// figure out how to terminate the program from here
} else {
System.out.println("Not a valid input, type 1-6");
printMenu();
}
} else {
userInput2.nextLine(); // <-- consume the non-number
System.out.println("Not a valid number, type 1-6");
printMenu();
}
Instead of "expecting" an int...
int userInt = userInput2.nextInt();
You should "expect" a String...
int actualInput = userInput2.nextLine();
From this you could then use Integer.parseInt(String) in an attempt to parse the String to an int, this will give you your first chance to validate the value.
The problem with this is Integer.parseInt(String) can throw a NumberFormatException, and you really should avoid making logic decisions based on exceptions.
Another approach might be to use a regular expression instead, something like...
if (actualInput.matches("^\\d*")) {
// This is a number, safe to parse to int
} else {
// This is not a number and is not safe to be parsed
}
Once you're satisfied that the actualInput is a number, you could use another Scanner to get the next int...
Scanner safeScanner = new Scanner(actualInput);
int userInt = safeScanner.nextInt();
as an example
userInt instanceof String will always be false, since userInt is an int. If it is an int, you don't need to check for instanceof string.
What you meant is to proof check the string from user input with StringUtils.isNumeric and reduce your other expressions to:
if 1<= userInt && userInt <=6
Should I even be using an instanceof statement? Is there a better way
to check for this?
Even if some other string, non numeric, could be converted to int and result into a value between 1 and 6, this way it would be rejected. Keep the numeric check, yes, just the correct one, StringUtils.isNumeric .
If you opt to have userInt as String, instead, then turn the other expression into if 1<= Integer.parseInt(userInt) && Integer.parseInt(userInt) <=6
First of all, after you write something like this:
int userInt = userInput2.nextInt();
your userInt is declared as an int, and can be nothing but a int. So writing something like this makes no sense:
userInt instanceof String
Because here, you already know that userInt is an int, because you declared it so.
The problem (where the exception, or crash as you called it, occurred) is elsewhere. It will happen at the call to nextInt().
Read the documentation for Scanner.nextInt(), under the exceptions, it states:
Throws:
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
So that is exactly what happens.
You have several choices, two of which:
catch the exception, and handle it the way you want it to be handled
Use of Scanner.hasNextInt().
I already see a growing number of alternative approaches.
Also most people would translate that chain of if/else if statements into a switch/case/default construct.
Then an other problem in your printMenu() method, it is endlessly recursive. Even though it is just user input, and it might have a limited timespan, with limited user entries before it exits, in theory you could reach a situation where you get a StackOverflowException. This implementation begs to be converted from recursive to iterative to avoid overallocation of objects (memory leak) and having a stack that grows forever.

Loop certain IF statements in java

import java.util.Scanner;
public class main {
public static void main(String[] args) {
int number = 0;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the number of sides");
number = input.nextInt();
if (number == 1) {
System.out.println("Circle");
}
if (number == 3) {
System.out.println("Triangle");
}
if (number == 4) {
System.out.println("quadrilateral");
}
else {
System.out.println("Incorrect Input");
}
}
}
Hello, I am trying to use the if statement. Can anyone advise me how to loop if statements? Because I get this as a result for example:
circle
Incorrect Input.
Also, How could I repeat the scanner so it allowed me to type another input?
Currently, the else clause is only associated to the last if block i.e. if (number == 4) {...} This means if any of the other if blocks are executed, it will still print "Incorrect Input". The solution is to use else if instead of separate if's.
if (number == 1) {
System.out.println("Circle");
}else if (number == 3) {
System.out.println("Triangle");
}else if (number == 4) {
System.out.println("quadrilateral");
}
else {
System.out.println("Incorrect Input");
}
You can use switch case (see : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html).
And you can check the type of number or string with instanceof.
For your second part question, I guess you're looking for something like a do....while loop, you can set up some condition like if the input result is not a number, then it will stuck in the loop until the user type in a number then only go in the the if, else-if statement

Is it possible to use a boolean expression in this manner?

I am currently learning java script and attempting new things, for example I wish to see if I can set a boolean expression to end if it detects a starting number through an ending number.
Or in other terms what I'll want is 3 through 8.
I will make it clear I am using netbeans IDE.
Within my last else if statement, I want it to end the asking process. I am hoping it is a simple fix, but I can not think of anything that will accomplish this unless I create a lot more else if statements.
Scanner input = new Scanner(System.in);
int[][] table;
boolean stopasking = true;
while (stopasking = true){
System.out.println("Let's make a magic Square! How big should it be? ");
int size = input.nextInt();
if (size < 0)
{
System.out.println("That would violate the laws of mathematics!");
System.out.println("");
}
else if (size >= 9)
{
System.out.println("That's huge! Please enter a number less than 9.");
System.out.println("");
}
else if (size <= 2)
{
System.out.println("That would violate the laws of mathematics!");
System.out.println("");
}
else if (size == 3)
{
stopasking = false;
}
}
You have used the assignmnent operator =
you should use == instead
also the condition size<=2 holds when size<0 so you can use one if for both
while(stopasking){
if (size <= 2) {
System.out.println("That would violate the laws of mathematics!\n");
} else if (size >= 9){
System.out.println("That's huge! Please enter a number less than 9.\n");
} else if (size == 3){
stopasking = false;
}
}
you can use the boolean expression in this way, as condition to exit from a loop. Some would say it is a more elegant solution than break.

Calling an Integer into a Boolean

The section of my code that isn't working
}else if(bossOne == 3){
int bossOneHP = 70 + bossStat.nextInt(10)-5;
int bossOneOP = 27 + bossStat.nextInt(10)-5;
int bossOneBP = 12 + bossStat.nextInt(10)-5;
String bossName = "Spartan King";
waits();
System.out.println("Your first enemy is the Spartan King.");
System.out.println("It has a very powerful attack, lets hope you have enough health.");
}
boolean keepPlaying = true;
while (keepPlaying){
Scanner choice = new Scanner(System.in);
System.out.println("Enter 1 to attack.");
System.out.println("Enter 2 to block.");
System.out.println("Enter 3 to exit the game.");
int selection = choice.nextInt();
if (selection == 1){
waits();
System.out.println("You attack.");
System.out.println(bossOneHP == bossOneHP - (OP - bossOneBP));
}else if(selection == 2){
waits();
System.out.println("You block.");
System.out.println(HP == HP - (bossOneOP - BP));
}else if(selection == 3){
break;
}
if (bossHP == (0 || >0){
System.out.println("Congratulations you won!");
break;
}
if (HP == (0 || >0)){
System.out.println("Sorry you lost.");
break;
}
I need to have the integers be called to the section. This code is for the meat of the game I am creating for my programming class any help would be appreciated.
You want the 'greater than or equal to' operator: >=
if (bossHP >= 0){
System.out.println("Congratulations you won!");
break;
}
if (HP >= 0){
System.out.println("Sorry you lost.");
break;
}
OK, so I'm not sure exactly what is supposed to do what but I will give it a try. It looks like you got a little confused.
For example:
if (selection == 1){
waits();
System.out.println("You attack.");
System.out.println(bossOneHP == bossOneHP - (OP - bossOneBP));
}else if(selection == 2){
In this code you seem to be printing to console a comparison of the variable bossOneHP and bossOneHP - (OP - bossOneBP), which I think is not what you intended as the statement would print true only if (OP-bossOneBP) was zero (basic algebra). What I suspected you intended:
if (selection == 1){
waits();
System.out.println("You attack.");
bossOneHP = bossOneHP - (OP - bossOneBP);
}else if(selection == 2){
This sets the variable bossOneHP to itself minus (OP minus bossOneBP). Note you can also do it like this:
if (selection == 1){
waits();
System.out.println("You attack.");
bossOneHP-= OP - bossOneBP;
}else if(selection == 2){
Which is faster. -= sets a value to itself minus the following value as opposed to = which just sets it to the new value. Also == does a comparison returning true if they are equal while = sets a variable to a new value.
Second issue:
if (bossHP == (0 || >0){
I am assuming you want to activate the if statement if bossHP is less than or equal to zero. The || statement is a boolean operator (It compares the two boolean inputs on either side, whether that be a variable or a comparison or a function, and returns a single boolean value of true if either input was true), and does not function like the word or. To compare two numbers (in this case the variable bossHP and zero), you use one of several operators. They are as follows:
== -returns true (which activates the if statement) if the numbers or objects (if they are the same instance, not if they contain equal values) on both sides are identical.
< -returns true if the left hand number is smaller than the right hand one (doesn't work on objects)
> -returns true if the right hand number is smaller
<= -returns true if the left hand number is smaller or equal to the right hand number
>= -returns true if the right hand number is smaller or equal to the left hand number
!= -returns true if the numbers or objects do not equal each other (effective opposite of the == token)
! -only takes one boolean on the right hand side and returns its opposite (this inverts the value essentially), if(!val) is equivalent and better to if(val == false)
The correct code would probably be something along the lines of:
if (bossHP >= 0){
System.out.println("Congratulations you won!");
break;
}
also instead of doing the while(keepPlaying) thing you could also do while(true) and run a break; command when 3 is inputed

Categories