190k views
2 votes
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.

User PJx
by
4.4k points

1 Answer

6 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner stdin = new Scanner(System.in);

String response = "";

while (true) response.equals("n"))

break;

}

}

Step-by-step 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

User Nikhil Kumar
by
4.1k points