I need to know why this java program won\'t compile. It won\'t recognize the var
ID: 655423 • Letter: I
Question
I need to know why this java program won't compile. It won't recognize the variable "count" in the return statement. "CAL.java:44: error: cannot find symbol" is the message that the compiler gives me.
import java.util.Random;
import java.util.Scanner;
public class CAL
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name");
name = keyboard.nextLine();
int count = Problem();
System.out.println(name+"you got "+count+"/5 right");
}
public static int Problem()
{
for (int n=1;n<6;n++)
{
int num1 = 0;
int num2 = 0;
int answer = 0;
int count = 0;
System.out.println(count);
Random r = new Random();
Scanner in = new Scanner(System.in);
num1 = r.nextInt(10);
num2 = r.nextInt(10);
System.out.println(num1 + " times " + num2 + " = ? ");
answer = in.nextInt();
if (answer == (num1*num2))
{
count++;
System.out.println("Good job! You got it right!");
}
else
{
for (int I=1; I<3; I++)
{
System.out.println("You got it wrong, try again!");
answer = in.nextInt();
}
}
} /* for loop to 5 */
return count;
} /* end of method Problem */
}
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class CAL { public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name");
name = keyboard.nextLine();
int count = Problem();
System.out.println(name+"you got "+count+"/5 right");
}
public static int Problem()
{
int count=0;
for (int n=1;n<6;n++) {
int num1 = 0;
int num2 = 0;
int answer = 0;
System.out.println(count);
Random r = new Random();
Scanner in = new Scanner(System.in);
num1 = r.nextInt(10);
num2 = r.nextInt(10);
System.out.println(num1 + " times " + num2 + " = ? ");
answer = in.nextInt();
if (answer == (num1*num2)) {
count++;
System.out.println("Good job! You got it right!");
} else {
for (int I=1; I<3; I++) {
System.out.println("You got it wrong, try again!");
answer = in.nextInt();
}
}
}
/* for loop to 5 */
return count;
}
/* end of method Problem */
}
Expected Output :
Enter your name
Chegg Tutor
0
0 times 1 = ?
0
Good job! You got it right!
1
2 times 8 = ?
16
Good job! You got it right!
2
0 times 4 = ?
0
Good job! You got it right!
3
0 times 0 = ?
0
Good job! You got it right!
4
4 times 7 = ?
28
Good job! You got it right!
Chegg Tutoryou got 5/5 right
BUILD SUCCESSFUL (total time: 52 seconds)