java - Given a String variable response that has already been declared, write some code that repeatedly reads a value from standard input into response until at last a Y or y or N or n has been entered.

ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.

Respuesta :

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

   

    Scanner stdin = new Scanner(System.in);

    String response = "";

 while (true) {

     System.out.print("Enter the response: ");

     response = stdin.nextLine();

     

     if (response.equals("Y") || response.equals("y") || response.equals("N") || response.equals("n"))

         break;

 }

}

}

Explanation:

Create a string variable called response

Create a while loop that iterates until a condition is specified to stop

Inside the loop:

Ask the user for the input

Check if input equals "Y", "y", "N" or "n". If input equals any of these letters, stop the loop. Otherwise, continue asking for a new response

ACCESS MORE