Evening all,
My assignment is as follows:
Consider a program that administers multiple-choice quizzes. The student needs to provide a response such as A or D. Your job will be to check for input errors in the student response string. Check that the string has length 1, and that it is a letter between A and the last valid choice for the problem.
Complete the following code:
public class Inputs
{
/**
Gets the choice that the user provided, or null if the
user didn't provide a valid choice.
@param input the user input
@param maxChoice the maximum valid choice, e.g. "D" if there
are four choices.
@return the user input if it was a valid choice (i.e. length 1
and between "A" and maxChoice), null otherwise
*/
public String getChoice(String input, String maxChoice)
{
// Your work here
}
}
The validation website it currently down, so here is what I did. Would some of you experts take a look and see if I did this correctly, please.
Thank you!
public class Inputs
{
/**
Gets the choice that the user provided, or null if the
user didn't provide a valid choice.
@param input the user input
@param maxChoice the maximum valid choice, e.g. "D" if there
are four choices.
@return the user input if it was a valid choice (i.e. length 1
and between "A" and maxChoice), null otherwise
*/
public String getChoice(String input, String maxChoice)
{
String aInput;
String amaxChoice;
aInput = input;
amaxChoice = maxChoice;
int choiceA = 1;
int choiceB = 2;
int choiceC = 3;
int choiceD = 4;
if (aInput.length() == 1)
{
if (aInput.equals("A"))
{
System.out.println("Valid answer: A");
}
else if (aInput.equals("B"))
{
System.out.println("Valid answer: B");
}
else if (aInput.equals("C"))
{
System.out.println("Valid answer: C");
}
else if (aInput.equals("D"))
{
System.out.println("Valid answer: D");
}
else
{
System.out.println("Invalid answer");
}
}
else
{
System.out.println("Please enter only one letter");
}
return aInput;
}
}