Error returning boolean in java -
I'm trying to do a practice but got stuck.
This class will ask the user whether he still wants to play with the answer yes or no and boolean
wants to come back.
public boolean playAgain () {char va; Boolean T = True; Boolean ry; While (T = true) {IO.outputln ("Want to play again? (Y / n)"); Va = IO.inputCharacter (); If ((VA == 'Y') || (VA == 'Y')) {RI = true; T = false; } And if ((VA == 'n') || (VA == 'n')) {ri = false; T = false; }} Returns RY;
This gives me the next error:
[error] variable ry may not have started
< P> Is it possible to assign variable ry
? (Because in any case it is being assigned in the loop)
I changed your code and Added some explanation:
Public boolean playgain () {char va; // You do not need any other variables while (true) {// endless loop ... only if the user enters "y" or "n" // then you avoid this loop and by answering each "yes" Should stop a "no" answer as IO.outputln ("Want to play again? (Y / n)"); Va = IO.inputCharacter (); If ((VA == 'Y') || (VA == 'Y')) {Back Truth; // Instantly return the answer ... there is no need to stop the loop} and if ((VA == 'n') || (VA == 'n')) {return false; // See the above ... instantly return answer}}}
without a loop ("no" or "Y" will not be answered as "answer"): < / P>
Public Boolean Playgain () {IO.outputln ("Want to play again? (Y / n)"); Char va = IO.inputCharacter (); If ((VA == 'Y') || (VA == 'Y')) {Back Truth; } return false; }
Comments
Post a Comment