In: Computer Science
Often we would like to make the user think we like him/her so we use a friendly prompt to get them to decide if they want to continue a loop. One can make a sentinel using an integer value outside of the expected input range but a more delightful way is to ask politely if: System.out.println("Do you want to play again?);
In order to do this we will need a scanner and a do-while loop. For the scanner sometimes the compiler seems to malfunction if the same scanner object is used for different data types so I prefer to use 2 scanner object- one for numbers and one for strings, rather than one object with a wrapper/parser.
So set up: Scanner again = new Scanner(system.in);
String another="";
do {
//whatever stuff you want to do like an inner loop to play a game like Hi-Lo
System.out.println("Do you want to play again?);
another=again.nextLine();
}
while(another.equalsIgnoreCase("yes") || another.equalsIgnoreCase("y"));
This is Java Please
import java.util.Scanner;
public class Prompt {
public static void main(String[] args) {
Scanner again = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
String another = "";
do {
System.out.println("Heads-1 or Tails - 0?");
int ch = sc.nextInt();
int x = (int) (System.currentTimeMillis() % 2);
if (x == ch) {
System.out.println("You won toss");
} else {
System.out.println("You lost toss");
}
System.out.println("Do you want to play again?");
another = again.nextLine();
}
while (another.equalsIgnoreCase("yes") || another.equalsIgnoreCase("y"));
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me