Question: This is part of a larger Java code I was working on. My code keeps getting stuck here and looping without being able to exit.
This is part of a larger Java code I was working on. My code keeps getting stuck here and looping without being able to exit. This code is meant to ask if they want to run the main code again. The while statement is there to ensure that only a capital Y or N is entered. With a Y the code runs again while an N will let the code finish.
import java.util.Scanner; public static String answers() { String answer; System.out.println(" Would you like to run the code again?"); System.out.println("Answer Y for yes and N for No"); answer = input.next(); while(!answer.equals("Y") || !answer.equals("N")) { System.out.println("Answer Y for yes and N for No"); answer = input.next(); } return answer; }
private static Scanner input = new Scanner(System.in); public static void main(String[] args) { String answer = answers(); while(answer.equals("Y")) { continue; } System.out.println("Goodbye!"); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
