when I compile this program it tells me that result might not have been initialized. i have the return types as return result shouldnt this work? can someone tell me how to fix this?
// CheckAnswer.java - This program checks to see if the answer to a test item is correct.
// Input: None.
// Output: Correct or incorrect.
import javax.swing.*;
public class CheckAnswer
{
public static void main(String args[])
{
int intAnswer = 2;
String stringAnswer = "Abraham Lincoln";
boolean boolAnswer = false;
String result;
checkQuestion(intAnswer);
System.out.println("The int answer is " + result);
checkQuestion(stringAnswer);
System.out.println("The String answer is " + result);
checkQuestion(boolAnswer);
System.out.println("The boolean answer is " + result);
System.exit(0);
} // End of main() method.
public static String checkQuestion(int answer)
{
String result;
if(answer==answer)
{
result="correct";
}
return result;
}
public static String checkQuestion(String answer)
{
String result;
if(answer.compareTo(answer)==0)
{
result="correct";
}
return result;
}
public static String checkQuestion(boolean answer)
{
String result;
if(answer=false)
{
result="correct";
}
return result;
}
} // End of CheckAnswer class.