+ 1

Error in the code

My java code(using vs code):- import java.util.Scanner; class Demo { public static void main(String[] args) { Scanner my_scan = new Scanner(System.in); System.out.print("How many numbers you have to add"); int cc; cc = my_scan.nextInt(); for(int dd = 1; dd<=cc; dd++){ System.out.print("Enter number "+dd+": "); int ee; int ff; ee = my_scan.nextInt(); ff+=ee; } System.out.print("The sum of "+cc+" numbers is "+ ff ); } } this code errors that ff can't resolved to a variable So, my question is how my error will solve Please help me.

4th Feb 2022, 6:07 AM
Seema Srivastava
Seema Srivastava - avatar
3 Answers
+ 3
Because you need to define ff outside the loop as it's a local variable to that loop so
4th Feb 2022, 6:15 AM
Mukesh Kumar
Mukesh Kumar - avatar
+ 3
Thanks for your help Mukesh Kumar
4th Feb 2022, 7:21 AM
Seema Srivastava
Seema Srivastava - avatar
+ 2
you have define ff inside the loop
11th Feb 2022, 1:17 PM
Shailendra Kumar
Shailendra Kumar - avatar