javaloopsrepeatcontinue

Java - Do you want to continue?(Y/N)


I want to write a simple loop for the program to go back and restart it again.

Just a simple 1 question program. Then the system ask if the user want to do it again. If the user inputs Y ... the program will loop it back to the beginning and run the entire program again. If the user inputs N, it exits.

import java.util.Scanner; // show them as code

public class HowToDoLoop {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("How much money do you want to have? ");
    double money = input.nextDouble();

    System.out.println("Ok, here is yours $" + money);

    System.out.println("Do you want to continue y or n");

Solution

  • String c = "";
    do{
        System.out.println("How much money do you want to have? ");
        double money = input.nextDouble();
    
        System.out.println("Ok, here is yours $" + money);
    
        System.out.println("Do you want to continue y or n");
        c = input.nextLine();
    
    }while(c.equalsIgnoreCase("Y"));