I'm trying to solve an assignment (I'm still very new to Java) and have combed through many resources to solve this conflict but still can't quite work it out.(NOTE: Tuna is my Scanner variable)
int counted, sum, counted1;
System.out.print("Enter your number to be calculated: ");
counted = tuna.nextInt();
counted1 =tuna.nextInt();
for(int counted=0;counted<=counted1;counted++){
System.out.println("The sum is: "+ counted);
}
}
}
Result is: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Duplicate local variable counted
The problem I am supposed to solve is:
Write a program to read in a number and sum up all the numbers from 1 to that number. For e.g, if the user key in 6, then the output is 21 (1+2+3+4+5+6).
ADDED: I read a question() that is rather similar but I did not make the smae mistake by declaring it before.
See Question&Answers more detail:os