[Java] error:can not find symbol. I don't know what the hell is happening

  • Context: Java 
  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Java Symbol
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 3K views
stonecoldgen
Messages
108
Reaction score
0
Code:
import java.util.Scanner;
import static java.lang.System.out;
import java.util.Random;

public class wolf {
public static void main (String[] args){

int hitpoints = 100;
int damage = 40; //un numero cualquiera, solo para hacer una prueba
Scanner keyboard = new Scanner(System.in);

out.println("You have encountered a wild wolf!");
out.println("Would you like to ATTACK or RUN?");

String wolfSituation = keyboard.next();
int wolfHP = 60;
int wolfDMG = 20;  //falta añadir la probabilidad de hit del wolf	


	if (wolfSituation.equals("RUN")){  //al correr, hay que hacer que el jugador queda en la misma casilla pero sin el evento especial
		int randomnessWolfRun = new Random().nextInt(10) + 1;
			if  (randomnessWolfRun <= 5){
				out.println("You have succesfully escaped from the wild wolf.");
			} else{
				int remainingHitPoints = hitPoints - wolfDMG;
				out.println("You have roughly managed to escape, but you have been hurt by the wild wolf.");
			}
	
	}

}
}

on the line
Code:
int remainingHitPoints = hitPoints - wolfDMG;
, this error appears. Actually what I wanted to do was
Code:
int hitPoints = hitPoints - wolfDMG;
because this is a game (just to make the longstory short).


Any help?
 
Physics news on Phys.org


I don't know Java, but the obvious guess is that hitpoints and hitPoints are two different variables.
 


Looks like a good guess. I don't know Java either, but a Google search for "java case sensitive" returns in the description for the very first hit, "When coding in Java it's important to remember that Java is case sensitive."
 


You have not defined the variable remainingHitPoints. Why would you expect the compiler to be able to find it, since it doesn't exist?

Wait ... I see that I'm probably wrong about that. Stating it as int on the same line where you manipulate it must define it in JAVA. If that's the case, then you would have a problem saying

int hitPoints = hitPoints - wolfDMG;

you would need instead to say

hitPoints = hitPoints - wolfDMG;

otherwise you would be double-defining hitPoints (since you defined it at the top of your code)
 


As phinds suggests, you can't define and initialize a variable to its own value. You need to do something like this:

int hitPoints = 0;
.
.
.
hitPoints = hitPoints - wolfDMG;

or

hitPoints -= wolfDMG;

Also, as Borek and jtbell point out, hitPoints and hitpoints are different variables.