I'm trying to make a simple text game just for fun. Java is a language I want to learn so making a game is a great start. I'm having problem with my if statement however.
This is an example of whats happening. This is only a few hours into the project so I know it needs a lot of cleanup and could probably be enhanced a lot but as I go through the Java tutorials I'm updating and fixing things as I go, so please only focus on the problem I'm having which is:
When I attack I display the murloc health at the end to see that it is indeed going down, but with each new attack it resets to 10. So the ecurHlth is only changing in the if statement and going back to 10 after the statement ends, how can I make the if statement update the actual variable?

import java.util.Scanner;
import javax.swing.JOptionPane;
public class Game
{

public static void main(String[] args) {
	Scanner playIn = new Scanner(System.in);
	int turn = 1+(int)(Math.random()*100);//will generate a random number from 1-100, if 50< Player goes first.
    int miss = 1+(int)(Math.random()*100);
    int eAtck = 1+(int)(Math.random()*6);
    int plCuHlth = 25;
    int ecurHlth = 10;
    String charName;
System.out.println("Please name your brave warrior");
    charName = playIn.nextLine();

WarriorClass Ontulmora = new WarriorClass();
	Ontulmora.changeName(charName);
	Ontulmora.introShow();
MurlocClass Murky = new MurlocClass();
int fballAtck = 6;
int meleeAtck = 3;
if(miss>=85){
	fballAtck = 0;
}else 
	fballAtck = 6;
System.out.println("You are strolling down the road towards Orgrimmar when an angry Murloc blocks the path, it's clear he will not budge. Time for a fight !");
System.out.println("Press ENTER to continue");
String blankone = playIn.nextLine();


int chAtck0 = 0;
if(turn>=50)
{
	System.out.println("You get the first attack against the Murloc, please choose an attack. Type 1 for melee attack or 2 for fireball. Fireball has a higher chance of missing");
	String chAtck = playIn.nextLine();	

	chAtck0 = Integer.parseInt(chAtck);
		if(chAtck0==1)
		{
			ecurHlth = Murky.strHlth-meleeAtck;
		}
else{ 
	if(fballAtck==0){
	ecurHlth = Murky.strHlth-fballAtck;
	System.out.println("Your fireball missed!");}
	else 
		ecurHlth = Murky.strHlth-fballAtck;
	
	System.out.println(ecurHlth);
	if(ecurHlth==0){
		System.out.println("You have defeated the angry murloc!");
	}
}
}

else
{
	System.out.println("The murloc got the jump on you and is attacking first !");
	plCuHlth = eAtck-Ontulmora.statHlth;
	System.out.print("The murloc slices you with his sharp claws !");
    System.out.println("You aren't hurt badly, but a little angry now !");
    if(Ontulmora.statHlth<=0){
    	System.out.println("You have died, Game Over, Try Again, you suck and smell bad!");
    }
}
if(turn>=50)
{
	System.out.println("Please choose your next attack. Type 1 for melee attack or 2 for fireball. Fireball has a higher chance of missing");
	String chAtck = playIn.nextLine();	

	chAtck0 = Integer.parseInt(chAtck);
		if(chAtck0==1)
		{
			ecurHlth = Murky.strHlth-meleeAtck;
		}
else{ 
	if(fballAtck==0){
	ecurHlth = Murky.strHlth-fballAtck;
	System.out.println("Your fireball missed!");}
	else 
		ecurHlth = Murky.strHlth-fballAtck;
	
	System.out.println(Murky.ecurHlth);
	if(ecurHlth==0){
		System.out.println("You have defeated the angry murloc!");
	}
}
}

else
{
	plCuHlth = eAtck-Ontulmora.statHlth;
	System.out.print("The murloc slices you with his sharp claws !");
    System.out.println("You aren't hurt badly, but a little angry now !");
    if(Ontulmora.statHlth<=0){
    	System.out.println("You have died, Game Over, Try Again, you suck and smell bad!");
    }

Recommended Answers

All 6 Replies

I'm having problem with my if statement however.

Can you explain what the problem is and which if statement you are having problems with?

how can I make the if statement update the actual variable?

Which variable is the "actual" variable?

Can you explain what the problem is and which if statement you are having problems with?
The if statements are at lines 45 and 79.

Which variable is the "actual" variable?

The actual variable is at Line 12
45
79

Can you explain what the problem is? What if statements are not working the way you want.
If you don't know where the variable is being changed or to what values, add some println statements to the code to print out its value at each place in your code that it is changed.

You need to clean up the alignment of the {}s in your code if you want anyone to be able to read it. They are all over the place. Some }s are hidden at the end of statements.

The problem is that when I change a variable in an if statement it changes back to its original value after the statement ends. I'll get to work on cleaning up the code and see what I come up with, if nothing else I'll post updated code.

when I change a variable in an if statement it changes back to its original value

If you print out the value of the variable at ALL the places its value is changed, the print outs should show you where the problem is.

Cleaning up my code helped me figure out where my problem was. I wasn't using my current health the right way, my second attack was still taking away from my original health instead of the current one. Thanks for the suggestion. I'll try to keep my code neat from now on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.