I have to write a code that first asks for year, then age, and calculates when they were born.
import java.util.*; // Library for
console input
public class birthYear {
public static void main(String[] args){
Scanner console = new Scanner(System.in);
int yearInput = console.nextInt(); // Define variable for year & get value
System.out.println("Please enter the current year: ");
int ageInput= console.nextInt();// Define variable for age & get value
System.out.println("Please enter your age: ");
} // end main methods
public static int calculate(int year, int age){ // Calculate birth year
int birthYear = calculate(yearInput,ageInput);
int calculation = year - age; // calculate birth year
return calculation;
System.out.print("You were born in "+ birthYear); // display to console
} // end of calculate method
} // end birthYear class
/**
* @purpose (Calculates the user's birth year
based on their current age and current year)
*
* @author (your name)
* @version (a version number or a date)
*/
I keep getting undeclared "yearInput,ageInput" and it wont compile. Any idea where i went wrong? thank you so much
In the definition of your calculate method, you have two parameters: an integer called year, and an integer called age. In your calculate method, you have the following line:
This line should really be called from inside your main method. In there, you have access to the yearInput and ageInput variables. Once you're inside the calculate method, they are out of scope.
I also noticed that inside the main method, you call the nextInt() methods before printing the "Please enter the current year". You should probably swap those lines as well. The final code would look like this: