I have created a math game that asks ten random questions and grades you at the end. I created an integer variable named score and initialized it as 0. In the if statement, you are awarded 10 points if you answer correctly. I figured since I have score + 10 inside the for loop then I shouldn't have to use increments for the scoring system. If anyone could point me in the right direction on why Java is telling me that the score variable is not being used, I would greatly appreciate it. Cheers from Texas!
package pkgnew;
import java.util.Scanner;
import java.util.Random;
public class New {
public static void main(String args[]) {
//Game 10x loop
for(int i = 0; i < 10; i++)
{
//Declare and construct variables
Random randomnum = new Random();
int fnum, snum;
int mathnumber = randomnum.nextInt(20);
int newnumber = randomnum.nextInt(20);
//Declare and construct variables of math problem
fnum = mathnumber;
snum = newnumber;
//Declare random operator variable
String[] operators = {"+" , "-", "*", "/" };
int randomIndex = randomnum.nextInt(3);
String symbol = operators[randomIndex];
//Initialize answer and score
int answer = 0;
int score = 0;
//Switch statement for random operator and question display
switch (symbol) {
case "+":
System.out.println(fnum + "+" + snum);
answer = fnum+snum;
break;
case "-":
System.out.println(fnum + "-" + snum);
answer = fnum-snum;
break;
case "*":
System.out.println(fnum + "*" + snum);
answer = fnum*snum;
break;
case "/":
System.out.println(fnum + "/" + snum);
answer= fnum/snum;
break;
}
//User input
Scanner serena = new Scanner(System.in);
int userAnswer = serena.nextInt();
//If user input = answer display "correct"
if (userAnswer == answer) {
System.out.println("Correct!");
score = + 10;
//If user input does not = answer display "wrong" and correct answer
} else {
System.out.print("Wrong! The correct answer is: " );
System.out.println(answer);
}
}
System.out.println("Game Over!");
System.out.println("Your score is:");
System.out.println(score);
}
}
I use Java 8 and NetBeans 8.0.