L
L
lerny2022-01-18 13:26:03
Java
lerny, 2022-01-18 13:26:03

How to fix error: cannot find symbol?

Learning Java, Compiling Code

public class Evolution {
  public static void main(String[] args) {
    Amoeba a1 = new Amoeba(5);
    a1.setChromosomes(5);
    a1.infoAmoeba();
  }
}

class Amoeba {
  static int Amount;
  static boolean established = false;
  public Amoeba(int n) {
    if (established) {
      Amount += 1;
    } else {
      Amount = n;
    }
    int num = Amount;
    established = true;
  }
  private int Chromosomes;

  public void setChromosomes(int x) {
    Chromosomes = x;
  }

  public void infoAmoeba() {
    System.out.println("Amoeba №: " + num + ":");	
    if (Chromosomes == 0) {
      System.out.println("Chromosomes: unknown " + "; " + "Amount: " + Amount + ";");	
    } else {
      System.out.println("Chromosomes: " + Chromosomes + "; " + "Amount: " + Amount + ";");
    }
  }
}

gives an error message
Evolution.java:28: error: cannot find symbol
                System.out.println("Amoeba тДЦ: " + num + ":");
                                                    ^
  symbol:   variable num
  location: class Amoeba
1 error

I searched the Internet for translation, but did not find it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-01-18
@lerny

Good afternoon
In the same place it is written in black and white that it cannot find the symbol num
System.out.println("Amoeba №: " + num + ":");
. The variable you have declared inside the public class and is not available in the method itself.
int num = Amount;
Read about variable scope

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question