Where do I have to declare the variables?


#1



https://www.codecademy.com/courses/learn-java/lessons/introduction-to-java/exercises/variables?action=lesson_resume


my code has an error
Variables.java:6: error: variable myNumber is already defined in class Variables
int myNumber =42;
^
Variables.java:7: error: variable isFun is already defined in class Variables
boolean isFun = true;
^
Variables.java:8: error: variable movieRating is already defined in class Variables
char movieRating ='A';
^
3 errors


public class Variables {
  	int myNumber;
		boolean isFun; 
		char movieRating; 
    int  myluckynumber =7;
    int myNumber =42;
    boolean isFun = true;
    char movieRating ='A';
  
	public static void main(String[] args) {
  
  
  }
	
}


#2

You should declare the variables only once. When a variable has been declared, you can use it without typing the date type before its name. So your code should look like this:

public class Variables {
int myNumber;
boolean isFun;
char movieRating;
int myluckynumber =7;
myNumber =42;
isFun = true;
movieRating ='A';

public static void main(String[] args) {

}

}

Sorry, my English is poor. I hope my answer will be helpful.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.