+ 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.
3 Answers
+ 3
Because you need to define ff outside the loop as it's a local variable to that loop so
+ 3
Thanks for your help Mukesh Kumar
+ 2
you have define ff inside the loop