Do while loop in java

do while control structure in java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dowhileloopexample;

import java.util.Scanner;

/**
 *
 * @author derdus
 */
public class DowhileLoopExample {
    public static Scanner input = new Scanner(System.in);
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        /*This program allows a user to add 2 numbers
        continously until the user decides to stop by typing
        an N (for no)
        */
        Scanner user_repsonse = new Scanner(System.in);
        int num1,num2,sum;
        String response="Y";
        do{
            System.out.println("Enter first number");
            num1 = input.nextInt();
            System.out.println("Enter second number");
            num2 = input.nextInt();
            sum = num1 + num2;
            System.out.println("Sum is " + sum);
            
            System.out.println("Do you want to perfom another addition? Reply with y or n");
            input.reset();
            response  = user_repsonse.nextLine();
            
        }while(response.equalsIgnoreCase("y"));
        
        System.out.println("Goodbye!");
        
    }
    
}
5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *